2016-12-26 101 views
1

我有兩種佈局。第一個只有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> 

回答

2

試試這個:

<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文件。

更新:

對於插入佈局編程可以使用this答案antonyt用戶:

加入當ViewViewGroup,可以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); 
+0

你能告訴我怎麼做同樣的務實? –

+1

看看[this](http://stackoverflow.com/a/6917818/6950238)answer –

0

你試過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

0

你可以使用「包括」用於合併兩個不同的佈局

<include android:id="@+id/layout" layout="@layout/first_layout" android:layout_gravity="center" />

相關問題