2016-12-14 82 views
0

我使用2個內部Linearlayouts將我的屏幕分爲2個部分。它看起來像這樣:2視圖並排與高度相同

Screenshot

第一內佈局有一個TextView和一個按鈕。第二個有一個ImageView和一個Button。現在我想爲TextView和ImageView獲取相同的高度,而無需爲layout_height設置固定值。左邊的按鈕也應該與右邊的按鈕一致。

這裏是我的xml:

<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="horizontal" 
      tools:context="de.dk.masterfi.ActMain"> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:orientation="vertical"> 


    <TextView 
     android:id="@+id/text" 
     android:layout_width="wrap_content" 
     android:layout_height="280dp" 
     android:background="@drawable/border" 
     android:padding="10dp" 
     android:text="@string/welcome"/> 

    <Button android:id="@+id/button2" android:layout_width="match_parent" 
      android:layout_height="wrap_content" android:text="Favoriten"/> 


</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="280dp" android:src="@drawable/training"/> 

    <Button android:layout_width="match_parent" android:layout_height="wrap_content" 
      android:text="Hauptmenü"/> 

</LinearLayout> 

回答

1

試試這個

android:layout_height = "0dp" 
android:layout_weight = "1.0" 

更改權重,請注意TextViewandroid:layout_weight="1"屬性和ImageView

<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="horizontal" 
     tools:context="de.dk.masterfi.ActMain"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_margin="20dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 
     <TextView 
      android:id="@+id/text" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@drawable/border" 
      android:padding="10dp" 
      android:text="@string/welcome"/> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Favoriten"/> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_margin="20dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 
     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:src="@drawable/training"/> 
     <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hauptmenü"/> 
    </LinearLayout> 
</LinearLayout> 
+0

哇,怎麼謝謝!很好的解決方案。 –

0

你的XML更改爲

<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="horizontal" 
     tools:context="de.dk.masterfi.ActMain"> 

<LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_margin="20dp" 
     android:layout_weight="1" 
     android:orientation="vertical"> 


     <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" 
      android:background="@drawable/border" 
      android:padding="10dp" 
      android:text="@string/welcome"/> 

     <Button android:id="@+id/button2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="Favoriten"/> 

</LinearLayout> 
<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_margin="20dp" 
    android:layout_weight="1" 
    android:orientation="vertical"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:src="@drawable/training" 
    android:layout_weight="1"/> 

    <Button android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hauptmenü"/> 

</LinearLayout> 

</LinearLayout> 
0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_trail" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="horizontal" 
    tools:context="com.nividbharat.educompanion.activities.TrailActivity"> 
    <RelativeLayout 
     android:id="@+id/relLayout1" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="8dp" 
     android:layout_marginTop="16dp" 
     android:layout_weight="0.5" 
     android:background="@android:color/black" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/button1" 
      android:background="@android:color/holo_red_dark" /> 
     <Button 
      android:id="@+id/button1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="Button1" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:id="@+id/relLayout2" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_marginBottom="16dp" 
     android:layout_marginLeft="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="16dp" 
     android:layout_weight="0.5" 
     android:background="@android:color/black"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/button2" 
      android:background="@android:color/holo_red_dark" /> 
     <Button 
      android:id="@+id/button2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:text="Button2" /> 
    </RelativeLayout> 
</LinearLayout> 

這是佈局樣子的預覽,

enter image description here