layout_gravity="bottom"不起作用。

3

大家好,我是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>

我在定位按钮方面遇到了问题。 我希望它位于屏幕底部并水平居中。 我知道我可以使用相对布局,但我决定检查是否可以使用线性布局来实现。看起来我不能... bottom属性被忽略了。


尝试使用线性布局将按钮置于上方。 - mohammadreza khalifeh
“bottom”在垂直方向的LinearLayout上不起作用。 - Steve M
请参见以下链接:https://dev59.com/D2w15IYBdhLWcg3wCXOJ - Steve M
5个回答

5
只需要做一个更改。
从最后一个按钮中删除这一行。
android:layout_gravity="bottom|center_horizontal"

在包含最后一个按钮的线性布局中添加此行。
android:gravity="bottom|center"

这是输出结果:

enter image description here

点击这个链接可以了解gravity和layout_gravity的确切区别。这肯定会对你未来的设计有所帮助。

1
android:gravity="bottom|center" 不是 android:layout_gravity - HelloSadness
啊,是的,对不起。我眼瞎了。现在它完美地运行了 ^^ - Mido
就像我在我的回答中所说的一样 :) (很高兴听到) - HelloSadness
你应该给@Anto的答案点赞,以及其他你认为有帮助的答案。这会鼓励更多人去帮助别人 :) Mido - Sudip Podder

1
尝试将主要布局封装在一个LinearLayout中,其中layout_weight = 1,layout_width =“match_parent”,layout_height =“match_parent”,并在另一个LinearLayout中放置底部的按钮,其中layout_width =“match_parent”,并将gravity设置为bottom | center_horizontal。

1
您可以在最后一个按钮之前添加此视图:
<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>

谢谢。我还删除了底部属性。 - Mido

1
这应该可以工作:

<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属性只适用于相对布局吗? - Mido

0

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


它移动的是按钮中的文本,而不是按钮本身。 - Mido

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接