【问题标题】:Android Layout Priority?Android布局优先级?
【发布时间】:2014-10-04 01:08:34
【问题描述】:

我学习了 Android XML,但在布局方面遇到了问题。 可以看到,我声明了2个线性布局和1个相对布局,所以顺序是Linear (1) - Relative (2) - Linear (3) 但是,当我运行它时,显示显示线性 (1) - 线性 (3) - 相对 (2)。

所以,我想知道布局中是否有优先级或规则:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffff0000"
    >
    <Button
        android:id="@+id/backBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Linear_Layout1 (1)" 
        />

    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff000000"
        >   
        <TextView
            android:id="@+id/text01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="Relative_layout1 (2)"
            android:textColor="#ff0000ff"
            android:textSize="19dp" 
            />
        <TextView
            android:id="@+id/text02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Mosquito"
            android:textColor="#ff00ff00"
            android:textSize="15dp"
            android:background="#ffff0000"
            android:layout_centerInParent="true"
            android:layout_toRightOf="@id/text01"
            />
        <LinearLayout 
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="#fff0f0f0"
            >
            <TextView
                android:id="@+id/text03"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Linear_layout2 (3)"
                />
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

【问题讨论】:

  • 这可能是因为RelativeLayout 的高度为match_parent,将其更改为wrap_content 并检查。而你的LinearLayout(2)RelativeLayout 里面。试着把它放在外面。
  • LinearLayout 中的第三个LinearLayout 定义以下属性..

标签: java android xml android-layout


【解决方案1】:

否 没有任何优先级。

您已将 android:layout_centerInParent="true" 设置为 text01 并且相对布局的任何子视图都没有对齐。

当您没有为相对布局的子视图分配任何对齐方式时,就会出现重叠。(并从下到上添加到堆栈中)

查看更新的视图,它在相对布局中从上到下添加视图

<Button
    android:id="@+id/backBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Linear_Layout1 (1)" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff000000" >

    <TextView
        android:id="@+id/text01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Relative_layout1 (2)"
        android:textColor="#ff0000ff"
        android:textSize="19dp" />

    <TextView
        android:id="@+id/text02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text01"
        android:layout_toRightOf="@id/text01"
        android:background="#ffff0000"
        android:text="Mosquito"
        android:textColor="#ff00ff00"
        android:textSize="15dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/text02"
        android:background="#fff0f0f0"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text03"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="Linear_layout2 (3)" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    试试这个方法,希望能帮助你解决问题。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ffff0000">
        <Button
            android:id="@+id/backBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Linear_Layout1 (1)"/>
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ff000000">
            <TextView
                android:id="@+id/text01"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="Relative_layout1 (2)"
                android:textColor="#ff0000ff"
                android:textSize="19dp"/>
            <TextView
                android:id="@+id/text02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Mosquito"
                android:textColor="#ff00ff00"
                android:textSize="15dp"
                android:background="#ffff0000"
                android:layout_centerInParent="true"
                android:layout_toRightOf="@id/text01"/>
    
        </RelativeLayout>
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:background="#fff0f0f0">
            <TextView
                android:id="@+id/text03"
                android:gravity="center"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Linear_layout2 (3)"/>
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多