【发布时间】:2014-10-18 10:22:16
【问题描述】:
我想实现这样的布局,其中用户有 2 个控制面板。他可以通过按键从第一个切换到第二个,反之亦然。
已经尝试使用 LayoutInflater,但是没有成功:/
为什么要使用 2 种不同的布局进行操作的主要原因是,按钮几乎位于相同的位置,所以我想防止在一个布局中出现所有混乱并创建 2 个单独的控制面板布局。
这是我的布局:
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)
}
编辑:
如图所示,假设活动从 Screen1 开始,一旦用户按下 Btn1,Screen2 就会出现...如您所见,只有控制面板已切换。
但是,它不会在应用程序开始时膨胀该布局...
提前感谢您的任何建议、提示...
【问题讨论】:
-
分享截图就好了
-
你可以使用 Fragments。拥有一个 Fragment 占位符并交换包含的 Fragment 很容易。
-
看起来碎片将是最好的主意...谢谢你们:)
-
@Creck 如果您使用
Fragments,还请查看ViewPager或anotherViewPager,而不是依赖按钮,滑动更好,并且当您调用@987654331 时它还会为您提供动画@就可以了。
标签: android android-layout android-activity layout-inflater android-inflate