充气布局

时间:2014-10-18 10:22:17

标签: android android-layout android-activity layout-inflater android-inflate

我想实现这样的布局,用户有2个控制面板。他可以通过按下按钮从第一个切换到第二个,反之亦然。

已经尝试过使用LayoutInflater,但没有成功:/

主要原因是,为什么使用2种不同的布局,按钮几乎位于同一位置,所以我想在一个布局中防止所有混乱,并创建2个独立的控制面板布局。 / p>

以下是我的布局:

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/control_panel_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="5">


        <!-- Here comes including layouts-->

    </RelativeLayout>

</LinearLayout>

control_panel_1.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_panel1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_action1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btn_action2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector2"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_action1"
        android:layout_marginRight="50dp"/>

    <Button
        android:id="@+id/btn_action3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector3"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/btn_action1"
        android:layout_marginLeft="50dp" />

</RelativeLayout>

control_panel_2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/control_panel1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_action3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector4"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/btn_action4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_action_selector5"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@+id/btn_action3"
        android:layout_marginTop="20dp"
        android:layout_marginRight="60dp"/>

</RelativeLayout>

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_root);
    RelativeLayout controlPanelLayout = (RelativeLayout) findViewById(R.id.control_panel_layout);


    //Inflate first control panel on activity start
    LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View controlPanel1 = layoutInflater.inflate(R.layout.control_panel_1.xml);

    controlPanelLayout.addView(controlPanel1)
}

编辑: enter image description here

如图所示,让我们说活动从Screen1开始,一旦用户按下Btn1,屏幕2出现......如您所见,只有控制面板已切换。

然而,它在应用程序开始时不会对该布局进行膨胀......

提前感谢任何建议,提示......

1 个答案:

答案 0 :(得分:0)

onCreateView() 中对控制面板进行充气,并在处理按钮时单击(更改面板)。代码应该有点像这样:

private void inflateYourPanel(int panelLayoutID, ViewGroup placeholder) {
        placeholder.removeAllViews();

        View view = LayoutInflater.from(mActivity).inflate(panelLayoutID, placeholder, false);

        //find your views and set values and listeners

        placeholder.addView(view);
    }


编辑:占位符可以是任何布局(control_panel_layout)在启动活动时膨胀等

您可能仍然可以更好地查看片段 - 它可能更适合您的目的并提供更多可扩展性