直接上代码吧:一直在纠结求各位大神点破:
一、这是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。
一、这是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。
解决方案 »
- 安卓如何得到wifi连接的网络名称?
- (新手)为什么就是获取不到EditText里面的值,登录不了。
- 怎么让Bulider点击屏幕空白的地方不消失呢
- new DialogInterface.OnClickListener();这是神马意思啊。晕!
- androidmanifest.xml中声名一个activity时,名字前面加不加点?
- ksoap2调用web service遇到HttpTransportSE.call就没法执行下去,求助!
- 这个是什么错误,有经验的人能否告知 谢谢了
- Android titlebar字体模糊
- 长按显示的对话框,如何新增一条
- android 卡拉ok打分系统
- NDK 多个.c文件,多个.so报错
- 使用ExpandableListview展开多项数据问题
<?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>????????????是这样的么
mLL = (MyLinearLayout) this.inflate(mContext, R.layout.relative, null);
addView(mLL);
}
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);
}
}标红这行算不算啊
我见别人也这么写过啊,但就是显示不出来。刚刚参照2楼做了,显示出来了,但是并不是按照我在RelativeLayout中定义的位置显示的。