【问题标题】:inflating layout充气布局
【发布时间】:2014-10-18 10:22:16
【问题描述】:

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

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

为什么要使用 2 种不同的布局进行操作的主要原因是,按钮几乎位于相同的位置,所以我想防止在一个布局中出现所有混乱并创建 2 个单独的控制面板布局。

这是我的布局:

ma​​in.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)
}

编辑:

如图所示,假设活动从 Screen1 开始,一旦用户按下 Btn1,Screen2 就会出现...如您所见,只有控制面板已切换。

但是,它不会在应用程序开始时膨胀该布局...

提前感谢您的任何建议、提示...

【问题讨论】:

  • 分享截图就好了
  • 你可以使用 Fragments。拥有一个 Fragment 占位符并交换包含的 Fragment 很容易。
  • 看起来碎片将是最好的主意...谢谢你们:)
  • @Creck 如果您使用Fragments,还请查看ViewPageranother ViewPager,而不是依赖按钮,滑动更好,并且当您调用@987654331 时它还会为您提供动画@就可以了。

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


【解决方案1】:

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)

仍然可能你最好看看 Fragments - 它可能更适合你的目的并提供更多的可扩展性

【讨论】:

  • 谢谢老哥的回复,看来最后我会使用你们提到的片段。
  • 我发布的代码仍然对我有用 - 无论如何它对动态内容很有用(列表由于某种原因无法完成工作)