【问题标题】:Android layout_gravity Bottom/Top in LinearLayout线性布局中的 Android layout_gravity 底部/顶部
【发布时间】:2017-09-10 22:40:26
【问题描述】:

关于堆栈溢出的“layout_gravity Bottom/Top in LinearLayout”有数百万个问题和答案,但我仍然没有解决我的问题。

我想把我的 EditText 放在最顶部,上面写着“请把我放在最上面”,把 TextView 放在最底部,上面写着“请把我放在最下面”。我的梦想很基础,但我无法实现!

谁能帮帮我?

这是我的 XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:weightSum="1">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="210dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Please place me at very top" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@android:color/background_light"
            android:text="TextView"
            android:layout_weight="0.19" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="248dp"
        android:layout_weight="0.70"
        android:background="@color/colorAccent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:weightSum="1">

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="62dp"
            android:layout_gravity="top"
            android:text="Button" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.16"
            android:background="@android:color/background_light"
            android:text="Please place me at very bottom" />

    </LinearLayout>

</LinearLayout>

这是我的输出:

【问题讨论】:

  • 为什么不直接使用 ConstraintLayout?
  • 换句话说,在使用权重时留下android:layout_height 通常是错误的。另外,您已经应用了重心,所以不清楚为什么任何东西应该在顶部或底部。
  • 因为这是一个使用 LinearLayout 创建的屏幕,我不想更改所有屏幕。我只想添加一个按钮。 (这个按钮应该放在底部)
  • 我没有说要改变所有屏幕,只有这个

标签: android android-layout layout-gravity


【解决方案1】:

如果 LinearLayout 不起作用,请不要强迫自己使用它。

  1. 嵌套线性布局和权重不利于性能。

  2. RelativeLayout(或 ConstraintLayout)自然是为了将东西放置在 ViewGroups 的锚点上

我假设您希望文本和按钮根据您的其他属性居中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:weightSum="2">


    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_light"
            android:layout_weight="1">

        <EditText
                android:id="@+id/editText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:layout_alignParentTop="true"
                android:inputType="textPersonName"
                android:text="Please place me at very top" />

        <TextView
                android:id="@+id/textView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@android:color/background_light"
                android:text="TextView" />

    </RelativeLayout>

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="@android:color/holo_red_light"
            android:orientation="vertical">

        <Button
                android:id="@+id/button"
                android:layout_width="match_parent"
                android:layout_height="62dp"
                android:layout_centerInParent="true"
                android:text="Button"/>

        <TextView
                android:id="@+id/textView2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@android:color/background_light"
                android:text="Please place me at very bottom"/>

    </RelativeLayout>

</LinearLayout>

或者,您只需要在第二个 LinearLayout 上使用 android:gravity="bottom",然后其中的所有视图都位于底部。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical"
              android:weightSum="1">

    <!-- Top Gravity -->
    <LinearLayout
            android:gravity="top"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="@android:color/holo_green_light"
            android:orientation="vertical"
            android:layout_weight="0.5">

        <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Please place me at very top"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_light"
                android:text="TextView"/>

    </LinearLayout>

    <!-- Bottom Gravity -->
    <LinearLayout
            android:gravity="bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.50"
            android:background="@android:color/holo_red_light"
            android:orientation="vertical">

        <Button
                android:layout_width="match_parent"
                android:layout_height="62dp"
                android:text="Button"/>

        <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/background_light"
                android:text="Please place me at very bottom"/>

    </LinearLayout>

</LinearLayout>

【讨论】:

    【解决方案2】:

    您已为第二个线性布局设置重力底部以将视图放置在底部。

    首先将这一行改为LinearLayout:

    android:gravity="center_vertical"
    

    收件人:

    android:gravity="top"
    

    在 Second LinearLayout 处更改这一行:

    android:gravity="center_vertical"
    

    收件人:

    android:gravity="bottom"
    

    注意:避免使用嵌套权重会降低性能。

    【讨论】:

    • 顶部的textview呢?
    猜你喜欢
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多