安卓萌新,最近在写一个自定义组件时不知道该怎么写了,求指教
我在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,照上面的写法并不能显示出来
有没有大神知道这个该怎么写啊?
我在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,照上面的写法并不能显示出来
有没有大神知道这个该怎么写啊?
解决方案 »
- 如何通过代码获取手机内置sd卡路径及大小
- Mediaplayer与SurfaceView 在程序切换到后台再恢复播放,有声音没图像!
- 各位大侠,这个界面是用什么控件设计的?
- OpenGL中的坐标单位是如何转换的?
- 如何在一个WebView点击链接后使用别一个Activity来打开内容
- EditText报错问题,请指教
- 点击链接后,系统会弹出提示“使用以下内容完成操作”
- 求大牛指教,截屏读取/dev/graphics/fb0文件framebuffer,颜色位移,颜色不正确的解决办法
- AlarmManager 如何在其他activity中关闭,貌似只能拿到当前设置的PendingIntent才可以关闭?
- BroadcastReceiver提示AlertDialog会导致apk奔溃
- 布局问题
- 关于Android截屏服务开发
在布局预览上我写在那个ContextArea里面的东西都跑到ContextArea外面去了,在调试的时候我调用getChildCount()也没有获取到东西
这个是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>
二是LinearLayoutuap设置style的话不知道要怎么搞
不同子项类别,显示不同的View,这个RecycleView天生合适,你应该了解下