如何在Android线性布局中使用50%的宽度和高度

时间:2018-08-07 18:07:05

标签: android

我想创建像这样的android布局:

IMAGE

我使用相对布局创建了此容器,但是我不能将第二个容器的宽度和高度设置为50%。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


    <android.support.constraint.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:weightSum="100">

            <ImageView
                android:layout_margin="5dp"
                android:layout_width="0dp"
                android:layout_weight="50"
                android:adjustViewBounds="true"
                android:layout_height="wrap_content"
                android:src="@drawable/akhbar"
                android:id="@+id/akhbar_panel" />

            <ImageView
                android:id="@+id/amozesh_panel"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="5dp"
                android:layout_weight="50"
                android:adjustViewBounds="true"
                android:src="@drawable/amozesh" />

        </LinearLayout>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

2 个答案:

答案 0 :(得分:3)

我认为对此视图更好的父级是ConstraintLayout,但是如果您必须使用LinearLayout,则可以这样做:

<?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="200dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#fac"
        android:text="one"/>

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

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#afc"
            android:text="two"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:background="#caf"
            android:text="three"/>

    </LinearLayout>

</LinearLayout>

enter image description here

答案 1 :(得分:1)

这是使用ConstraintLayout解决此问题的一种方法:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="200dp">

    <TextView
        android:id="@+id/one"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#fac"
        android:text="one"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/two"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/two"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#afc"
        android:text="two"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/three"/>

    <TextView
        android:id="@+id/three"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:background="#caf"
        android:text="three"
        app:layout_constraintTop_toBottomOf="@+id/two"
        app:layout_constraintLeft_toRightOf="@+id/one"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</android.support.constraint.ConstraintLayout>
相关问题