安卓萌新,最近在写一个自定义组件时不知道该怎么写了,求指教
我在xml页面中有区域块,类似下面的代码<LinearLayout
style="@style/mgs_area1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"> <TextView
style="@style/mgs_area1_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/detail_h2"
android:textSize="18sp" /> <LinearLayout
style="@style/mgs_area2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

//从这个开始每个区域不一样
<TableRow></TableRow>
<xxxxx></xxxxx>


</LinearLayout>

</LinearLayout>
但每个区域代码块的结构都差不多,我想把相同的部分抽离出来成一个自定义组件,类似下面这样
<com.my.hy.pingtan.common.view.ContextArea
android:layout_width="match_parent"
android:layout_height="wrap_content">

//从这个开始每个区域不一样
<TableRow></TableRow>
<xxxxx></xxxxx>

</com.my.hy.pingtan.common.view.ContextArea>
这个组件是有子项的,而且子项有可能不同,有的是TableRow,有的是GridLayout,照上面的写法并不能显示出来
有没有大神知道这个该怎么写啊?

解决方案 »

  1.   

    你的ContextArea有bug吧,你可以先将View固定大小然后设置个背景色,看看是否有加载到布局中,一层层排查
      

  2.   

    没有报错
    在布局预览上我写在那个ContextArea里面的东西都跑到ContextArea外面去了,在调试的时候我调用getChildCount()也没有获取到东西
      

  3.   

    你自定义的ContextArea有问题,伪代码发来
      

  4.   


    这个是java类public class ContextArea extends LinearLayout {    private TextView titleText;
        private LinearLayout linearLayout;    public ContextArea(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);        // 加载布局
            View view = LayoutInflater.from(context).inflate(R.layout.comm_context_area, this);        titleText = (TextView) view.findViewById(R.id.context_area_title);
            linearLayout = (LinearLayout) view.findViewById(R.id.context_area_linearLayout);
        }    //设置标题
        public void setTitleText(String title) {
            this.titleText.setText(title);
        }    public TextView getTitleText() {
            return titleText;
        }    @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            if (getChildCount()>2){
                for (int i=0;i<getChildCount();i++){
                    Log.d("11111111111111111111111",getChildAt(i).getId()+"");
                }
            }
        }
    }
    这个是对应的comm_context_area.xml页面<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/mgs_area1">    <TextView
            android:id="@+id/context_area_title"
            style="@style/mgs_area1_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/detail_user_h2"
            android:textSize="18sp" />    <LinearLayout
            android:id="@+id/context_area_linearLayout"
            style="@style/mgs_area2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">    </LinearLayout></LinearLayout>
      

  5.   

    view的构造4个至少重写前3个:1参、2参、3参的,
      

  6.   

    套个RecycleView,按情况分别实现子项
      

  7.   

    这是用什么软件开发的,然后怎么编译成apk
      

  8.   

    有官方工具的,Android Studio
      

  9.   

    最后解决方法是java类不指定xml(不加载布局文件),然后再代码里动态生成,感觉一旦指定了xml文件,在引用的地方就不能加子项了最后残留了两个问题,一个是TextView设置style,虽然可以调用setTextAppearance()方法,但只支持Android 6.0及以上版本,低了就行
    二是LinearLayoutuap设置style的话不知道要怎么搞
      

  10.   

    View 的构造函数有好几个,你用三个或者4个参数的构造函数试试,里面有指定样式的。
    不同子项类别,显示不同的View,这个RecycleView天生合适,你应该了解下