2个视图并排,高度相同

时间:2016-12-14 18:51:42

标签: android android-layout android-linearlayout

我使用2个内部Linearlayouts将我的屏幕划分为2个部分。它看起来像这样:

Screenshot

第一个内部布局有一个TextView和一个Button。第二个有一个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>

4 个答案:

答案 0 :(得分:1)

试试这个,请注意android:layout_weight="1"TextView的{​​{1}}属性:

ImageView

答案 1 :(得分:0)

尝试this

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

根据您的规格更改重量

答案 2 :(得分: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>

答案 3 :(得分: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

相关问题