【问题标题】:Spread three linear layouts in one parent relative layout在一个父相对布局中展开三个线性布局
【发布时间】:2018-10-18 00:19:13
【问题描述】:

我有以下代码,其中我希望三个线性布局分布在父相对布局中,就像单个线性布局中的子视图分布在线性布局中一样。我尝试在线性布局标签中使用 'layout_weight="1"' 但没有得到预期的结果。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root_relative_layout">

    <LinearLayout
        android:id="@+id/first_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="#fff">

        <ImageView
            android:id="@+id/cat_paw_image"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:contentDescription="@string/image_of_cat_paw"
            android:src="@drawable/cat_paw" />

        <ImageView
            android:id="@+id/lion_image"
            android:contentDescription="@string/image_of_lion"
            android:layout_width="match_parent"
            android:layout_height="212dp"
            android:src="@mipmap/lion"/>

        <TextView
            android:id="@+id/work_for_lions"
            android:text="@string/for_lion"
            android:contentDescription="@string/for_lion"
            android:textSize="30sp"
            android:textColor="#111"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_marginBottom="10dp"/>

    </LinearLayout>



    <LinearLayout
        android:id="@+id/second_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#fff"
        android:layout_below="@id/first_linear_layout">

        <ImageView
            android:id="@+id/location_logo"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:contentDescription="@string/location_logo"
            android:src="@drawable/location_logo"
            android:layout_weight="1"/>

        <TextView
            android:id="@+id/first_text_view_in_second_linear_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/where_is"
            android:textColor="#111"
            android:textSize="25sp"
            android:layout_marginTop="5dp"
            android:layout_weight="1"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/third_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#fff"
        android:layout_below="@id/second_linear_layout">

        <ImageView
            android:id="@+id/time"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:contentDescription="@string/time_image"
            android:src="@drawable/time"
            android:layout_marginLeft="25dp"/>

        <TextView
            android:id="@+id/first_text_view_in_third_linear_layout"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="@string/what_the_time"
            android:textSize="25sp"
            android:textColor="#111"
            android:textAlignment="center"/>
    </LinearLayout>



</RelativeLayout> 

注意:我是 android 开发的初学者。

【问题讨论】:

标签: android android-layout


【解决方案1】:

使用相对布局时,您必须指定线性布局相对于相对布局或彼此的位置,请参阅Relative Layout doc。或者使用线性布局作为基础Linear Layout doc

甚至看看 ConstraintLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    • 1970-01-01
    相关资源
    最近更新 更多