【问题标题】:Gravity in LinearLayout - android?LinearLayout中的重力 - android?
【发布时间】:2017-01-16 01:55:28
【问题描述】:

我创建了下面的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeStart"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back"
    tools:context=".SongsActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/header"
        android:orientation="vertical">

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

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/felesh_m" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal|center_vertical"
                android:gravity="center_horizontal|center_vertical"
                android:orientation="vertical">

                <ImageButton
                    android:id="@+id/imbCenter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|center_vertical"
                    android:background="@null"
                    android:src="@drawable/hosein_hlarge" />
            </LinearLayout>

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                android:src="@drawable/felesh_m" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4" />
    </LinearLayout>
</RelativeLayout>

见下图:

【问题讨论】:

  • 在下面查看我的答案,如果您遇到任何问题,请告诉我。

标签: android-layout android-linearlayout android-gravity


【解决方案1】:

像这样改变你的布局。您不需要采取多个Linear Layouts。这对性能不利。所以我将其更改为带有一些 Weight 属性的单个 Linear Layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relativeStart"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/common_ic_googleplayservices">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/common_ic_googleplayservices"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:orientation="horizontal">

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.50"
                android:background="@null"
                android:src="@drawable/common_ic_googleplayservices" />


            <ImageButton
                android:id="@+id/imbCenter"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:background="@null"
                android:src="@drawable/common_ic_googleplayservices" />

            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.50"
                android:background="@null"
                android:src="@drawable/common_ic_googleplayservices" />
        </LinearLayout>

        <android.support.v7.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="4" />
    </LinearLayout>
</RelativeLayout>

【讨论】:

  • RelativeLayout 或第一个 LinearLayout 不是必需的。我会删除两个 LinearLayout,因为所有这些都可以通过使用单个 RelativeLayout 来完成。为您的 UI 提供更快的响应速度
  • @Rotwang 先生,他在垂直方向拍摄了Recycler View,然后他想要在水平方向拍摄更多 3 张图像,这就是为什么他可能有这样的设置。
  • @JoJoRoid 如果对您有帮助,请接受并投票。
  • 再次重申:所有这些都可以通过使用单个 RelativeLayout 来完成。如果你知道如何使用它。
  • @Rotwang 先生,您说得对,可以使用单个 Relative Layout 来完成。但是到目前为止,我对 OP 先生才有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2018-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多