android-layout - LinearLayout 中的重力 - android?

标签 android-layout android-linearlayout android-gravity

我创建了以下布局:

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

见下图:

enter image description here

最佳答案

像这样改变你的布局。您不需要采用多个线性布局。这对性能不利。因此,我将其更改为带有一些 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>

enter image description here

关于android-layout - LinearLayout 中的重力 - android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385749/

相关文章:

android - 在textview中更改可绘制图像的重力

android - 关闭键盘时渐变背景变白(仅当 ListView 有项目时)

java - 如何在android中裁剪位图的重叠区域?

android - 定义布局内容区域

android - 如何在 Horizo​​ntalScrollView 中设置线性布局宽度?

java - 设置高度 View 以匹配父级与android中的动画

android-layout - 当我打开微调器时,沉浸式全屏模式(粘性标志)被禁用

Android:如何在 Activity (不是 fragment )之间滑动,主/细节最佳设置

android - Android中的gravity和layout_gravity有什么区别?

Android - 更改 TextInputLayout 的位置 float 标签(底部 float 标签)?