Android自定义Layout嵌套问题.

goddezz goddezz 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
直接上代码吧:一直在纠结。。。求各位大神点破:
一、这是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。
...全文
评论 8
收藏 已收藏
分享
返回顶部
goddezz
Android 80,490 成员 91,375 内容