如何根据第二个布局的特定视图将一个布局与另一个布局合并?
我有两种布局。第一个只有TextView
,第二个有下面的代码。如何根据第二个布局的特定视图将一个布局与另一个布局合并?
我想合并这两个布局的方式,第一个布局显示前面提到的第二个布局的id为“arry”的TextView
。我使用FrameLayout
这样做,但为此我必须为第一个布局设置顶部边距。
我不想这样做,因为第一个布局会根据屏幕大小的变化而上下移动。任何人都可以告诉我如何在不设置这种边界的情况下做到这一点?我想获得TextView
的位置,之后我想以编程方式设置第一个布局。这里是上述代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.user_17.hashmapbasics.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
试试这个:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.user_17.hashmapbasics.MainActivity">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<include
android:id="@+id/first_layout_id"
layout="@layout/first_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/arry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView
android:id="@+id/arry1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
其中first_layout
- 你的第一个布局xml
文件。
更新:
加入当
View
到ViewGroup
,可以specify an index其设置视图的 位置在父母。你有两个意见,所以(从零开始计算)你想在第一个位置添加 ;只需拨打
ll.addView(view, 1)
;让它在两个TextView之间放置 。
在你的情况你应该使用secondLayout.addView(firstLayout, 2)
,其中firstLayout
是你的第一个布局Layout
对象:
LayoutInflater inflater = LayoutInflater.from(context);
View firstLayout= inflater.inflate(R.layout.first_layout, null, false);
secondLayout.addView(firstLayout, 2);
你试过include
布局?
如果你定的布局XML文件,假设“your_layout.xml
” 然后你可以去你的布局,其中TextView
然后仅低于添加 这一行:
<include layout="@layout/your_layout"/>
例如:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/your_parent_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/app_bg"
android:gravity="center_horizontal">
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
<include layout="@layout/your_layout"/>
</LinearLayout>
更多了解,请https://developer.android.com/training/improving-layouts/reusing-layouts.html
你可以使用“包括”用于合并两个不同的布局
<include android:id="@+id/layout" layout="@layout/first_layout" android:layout_gravity="center" />
你能告诉我怎么做同样的务实? –
看看[this](http://stackoverflow.com/a/6917818/6950238)answer –