我main.xml中布局只包含两个按钮和内容区域,这下图为:如何在我的情况按下按钮时更新内容? (翼状特征)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/myBtns"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/btn_one"
android:layout_width="100dip"
android:layout_height="30dip"
android:text="button one"
/>
<Button
android:id="@+id/btn_two"
android:layout_width="100dip"
android:layout_height="30dip"
android:text="button two"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/content_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<!--different button press will embed different content here-->
</LinearLayout>
</LinearLayout>
我想创建自己的制表像功能,每个按钮按下将更新按钮下方的内容(content_area
)。所以我准备了两个其他内容布局:
content_one.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView.../>
<Button .../>
</LinearLayout>
content_two.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Gallery.../>
<Button .../>
</LinearLayout>
用我的简单代码上面显示,我想实现以下功能:
in main.x毫升:
被按下时
button_one
,content_one.xml将被包埋到content_area
的main.xml中;当按下
button_two
,main.xml中的content_area
将被更新以content_two.xml
使用按钮以创建薄片形特征这意味着。
我的问题是如何更新已经内嵌我main.xml中布局content_area
内外部布局的xml文件( & content_two.xml例如content_one.xml)的content_area?
这就是:
button_one.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//What to do here?? to update the content_area in main.xml with an external xml layout
}
});
---------------- UPDATE ------------------- ---------
我想:
button_one.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
LayoutInflater inflater = (LayoutInflater)MyActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = inflater.inflate(R.layout.content_one, null);
LinearLayout contentArea= (LinearLayout) findViewById(R.id.content_area);
contentArea.addView(inflatedView);
}
});
但它不工作,为什么?
此之前的回答可以帮助你:http://stackoverflow.com/questions/5961522/help-how-can-i-do-includein-xml-programmatically-in-my-app-android – Fortega
@ Fortega,我试过但是不行,看我的更新 – Leem