【问题标题】:layout_gravity="bottom" doesn't worklayout_gravity="bottom" 不起作用
【发布时间】:2016-10-17 20:30:05
【问题描述】:

你好 stackoverflow 社区! 我在编程和这个网站上都是新手,但让我们开始吧。 我写了一个应用程序:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:orientation="horizontal"
    android:layout_marginTop="10dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="Team A"
            android:textSize="14sp"
            android:fontFamily="sans-serif-medium"/>

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0"
            android:textSize="56sp"
            android:textColor="#000000"
            android:fontFamily="sans-serif-light" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus3a"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus2a"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus1a"
            android:text="Free throw" />
    </LinearLayout>

    <View
        android:layout_width="1dp"
        android:background="@android:color/darker_gray"
        android:layout_height="match_parent">

    </View>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:orientation="vertical"
        tools:context="com.example.android.courtcounter.MainActivity"
        android:padding="24dp">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="Team B"
            android:textSize="14sp"
            android:fontFamily="sans-serif-medium"/>

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:padding="4dp"
            android:text="0"
            android:textSize="56sp"
            android:textColor="#000000"
            android:fontFamily="sans-serif-light"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus3b"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus2b"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:onClick="plus1b"
            android:text="Free throw" />
    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:onClick="scoreReset"
        android:text="reset" />
    </LinearLayout>

</LinearLayout>

我在最后放置按钮时遇到问题。 我希望它位于屏幕底部并水平居中。 我知道我可以使用相对布局,但我决定检查是否可以使用线性布局来做到这一点。而且似乎我不能......“底部”属性被忽略了。

【问题讨论】:

标签: android xml android-layout gravity


【解决方案1】:

只需进行一项更改。

从最后一个按钮中删除这一行

android:layout_gravity="bottom|center_horizontal"

并在包含最后一个按钮的线性布局中添加这一行

android:gravity="bottom|center"

这是输出:

Follow this link 以获得关于重力和布局重力的清晰概念。它肯定会在您即将推出的设计中为您提供帮助。

【讨论】:

  • android:gravity="bottom|center" 不是 android:layout_gravity
  • 啊,是的,对不起。我瞎了。现在它工作得很好^^
  • 就像我在 aswer 中所说的 :)(很高兴听到)
  • 您也应该投票赞成@Anto 的答案以及您认为有帮助的所有其他答案。它鼓励一个人更多地帮助他人:) Mido
【解决方案2】:

您可以在最后一个按钮之前添加此视图:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Add this -->
    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:onClick="scoreReset"
        android:text="reset" />
</LinearLayout>

【讨论】:

  • 谢谢。我还删除了底部属性。
【解决方案3】:

这应该有效:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical"
  android:gravity="bottom|center">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center|bottom"
    android:onClick="scoreReset"
    android:text="reset"/>

</LinearLayout>

【讨论】:

  • 难道 android:layout_alignParentBottom 只适用于相对布局?
【解决方案4】:

使用gravity="bottom" 代替layout_gravity 应该会有帮助。

【讨论】:

  • 它在按钮中移动文本,而不是按钮本身。
【解决方案5】:

尝试将主布局封装在一个具有 layout_weight = 1、layout_width = "match_parent"、layout_height = "match_parent" 的 LinearLayout 中,并将底部的按钮封装在另一个具有 layout_width="match_parent" 且重力设置为 bottom|center_horizo​​ntal 的 LinearLayout 中。

【讨论】:

    猜你喜欢
    • 2012-08-24
    • 2023-04-07
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多