简体   繁体   English

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

[英]how to use 50% width and height in android linear layout

I want to create android kind of layout like this : 我想创建这样的android布局:

IMAGE 图片

I created same this with relative layout but I can't place second container with 50% width and height. 我使用相对布局创建了此容器,但无法将第二个容器的宽度和高度设置为50%。

This is my code: 这是我的代码:

<?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>

I think a better parent for this view would be ConstraintLayout , but if you have to use LinearLayout then you can do this: 我认为此视图的更好的父对象是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>

在此处输入图片说明

Here's a way to solve it using ConstraintLayout: 这是使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM