kon*_*kea 7 android android-layout
我有第一个和第二个布局,我想先插入第二个.我想将第二个布局插入到具有id @ + id/layout的布局中,当我按下按钮获取布局时,它会显示按钮底部的第二个布局
第一个布局
<LinearLayout 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"
android:orientation="vertical" >
<Button
android:id="@+id/btn_get_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Layout" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
第二个布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/card_base"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/img_cover"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:scaleType="fitXY"
android:src="@drawable/card_beauty" />
<ImageView
android:id="@+id/img_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="20dp"
android:scaleType="fitXY"
android:src="@drawable/sample_0" />
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
Col*_*ire 23
如果我理解你,你应该在你的第一个布局中使用以下代码
<include
layout="@layout/second_layout"
android:id="@+id/includedLayout"
android:visibility="gone"
android:layout_below="@id/buttonId" />
Run Code Online (Sandbox Code Playgroud)
然后在按钮的动作中你只需使用
((RelativeLayout)findViewById(R.id.includedLayout)).setVisibility(View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)
Sam*_*eer 11
LinearLayout placeHolder = (LinearLayout) findViewByid(R.id.layout);
getLayoutInflater().inflate(R.layout.second_layout, placeHolder);
Run Code Online (Sandbox Code Playgroud)
include
这是一个更完整的例子.使用方形蓝色布局插入主布局include
.
activity_main.xml中
这是主要布局.使用引用自定义布局include
.您也可以在此处覆盖任何属性.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<!-- Here is the inserted layout -->
<include layout="@layout/my_layout"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
my_layout.xml
这是将插入主布局的自定义布局.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="5dp"
android:text="My Layout"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)