Android自定义Layout嵌套问题.

goddezz 2018-04-08 04:25:13
直接上代码吧:一直在纠结。。。求各位大神点破:
一、这是MainActivity:
public class MainActivity extends Activity {
private MyRelativeLayout mRL;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRL=(MyRelativeLayout)LayoutInflater.from(this).inflate(R.layout.activity_main, null);
setContentView(R.layout.activity_main);
}
}



二、这是MyActivity要显示的MyRelativeLayout
public class MyRelativeLayout extends RelativeLayout {
MyLinearLayout mLL;
private Context mContext;

public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.mContext=context;

// TODO Auto-generated constructor stub
}

@Override
public void onFinishInflate(){
super.onFinishInflate();
init();

}
public void init(){
mLL=(MyLinearLayout)this.inflate(mContext,R.layout.relative,null);
}

}



三、这是MyRelativeLayout对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<com.example.viewtest.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<com.example.viewtest.MyLinearLayout
android:id="@+id/layout"
android:layout_width="200px"
android:layout_height="200px"
android:background="#ff0000">

</com.example.viewtest.MyLinearLayout>


<ImageView
android:layout_below="@id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.example.viewtest.MyRelativeLayout>



四、这是嵌套的MyLinearLayout:
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
}



五、这个嵌套MyLinear对应的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<com.example.viewtest.MyLinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

</com.example.viewtest.MyLinearLayout>




问题是:
现在不现实嵌套的MyLinearLayout中的TextView。
...全文
764 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Yunc2 2018-04-09
  • 打赏
  • 举报
回复
你 MyLinearLayout 里面什么东西都没有加啊? 你xml 里面的<com.example.viewtest.MyLinearLayout 只是引用的自定义类,你这个类又没有添加任何子View或者关联layout
Yunc2 2018-04-09
  • 打赏
  • 举报
回复
MyRelativeLayout 里面都infalte 了,MyLinearLayout 为什么不 infalte ?
goddezz 2018-04-09
  • 打赏
  • 举报
回复
引用 3 楼 moonFY 的回复:
RelativeLayout 中不应该是 include 的吗?
我见别人也这么写过啊,但就是显示不出来。刚刚参照2楼做了,显示出来了,但是并不是按照我在RelativeLayout中定义的位置显示的。
goddezz 2018-04-09
  • 打赏
  • 举报
回复
引用 4 楼 u013894546 的回复:
你 MyLinearLayout 里面什么东西都没有加啊? 你xml 里面的<com.example.viewtest.MyLinearLayout 只是引用的自定义类,你这个类又没有添加任何子View或者关联layout
public class MyRelativeLayout extends RelativeLayout { MyLinearLayout mLL; private Context mContext; public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); this.mContext=context; // TODO Auto-generated constructor stub } @Override public void onFinishInflate(){ super.onFinishInflate(); init(); } public void init(){ mLL=(MyLinearLayout)this.inflate(mContext,R.layout.relative,null); } } 标红这行算不算啊
goddezz 2018-04-09
  • 打赏
  • 举报
回复
引用 1 楼 xj396282771 的回复:
你的activity_main布局是: <?xml version="1.0" encoding="utf-8"?> <com.example.viewtest.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.viewtest.MyLinearLayout android:id="@+id/layout" android:layout_width="200px" android:layout_height="200px" android:background="#ff0000"> </com.example.viewtest.MyLinearLayout> <ImageView android:layout_below="@id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.example.viewtest.MyRelativeLayout> 你的relative布局是: <?xml version="1.0" encoding="utf-8"?> <com.example.viewtest.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </com.example.viewtest.MyLinearLayout> ????????????是这样的么
不是啊 Activity就是一个RelativeLayout,RelativeLayout里套的一个LinearLayout。
moonFY 2018-04-08
  • 打赏
  • 举报
回复
RelativeLayout 中不应该是 include 的吗?
Jing丶無雙 2018-04-08
  • 打赏
  • 举报
回复
你把你MyRelativeLayout类的init方法这样写,你虽然引入了外部布局但是没有添加 public void init() { mLL = (MyLinearLayout) this.inflate(mContext, R.layout.relative, null); addView(mLL); }
Jing丶無雙 2018-04-08
  • 打赏
  • 举报
回复
你的activity_main布局是: <?xml version="1.0" encoding="utf-8"?> <com.example.viewtest.MyRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.viewtest.MyLinearLayout android:id="@+id/layout" android:layout_width="200px" android:layout_height="200px" android:background="#ff0000"> </com.example.viewtest.MyLinearLayout> <ImageView android:layout_below="@id/layout" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </com.example.viewtest.MyRelativeLayout> 你的relative布局是: <?xml version="1.0" encoding="utf-8"?> <com.example.viewtest.MyLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </com.example.viewtest.MyLinearLayout> ????????????是这样的么

80,360

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧