首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Layout_gravity=“底部”不起作用

Layout_gravity=“底部”不起作用
EN

Stack Overflow用户
提问于 2016-10-17 20:30:05
回答 5查看 10.9K关注 0票数 3

你好,堆栈溢出社区!我在编程和这个网站上都是新手,但让我们谈谈重点。我写了一个应用程序:

代码语言:javascript
复制
    <?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>

我有一个问题,定位一个按钮在最后。我希望它是在一个屏幕的底部,并以水平居中。我知道我可以使用相对布局,但我决定检查是否可以使用线性布局。似乎我不能..。“底部”属性被忽略。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-10-17 20:44:25

只要做一次改变。

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

代码语言:javascript
复制
android:layout_gravity="bottom|center_horizontal"

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

代码语言:javascript
复制
android:gravity="bottom|center"

这是输出:

Follow this link对重力和layout_gravity有一个清晰的认识。它肯定会在你即将到来的设计中对你有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2016-10-17 20:33:50

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

代码语言:javascript
复制
<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>
票数 1
EN

Stack Overflow用户

发布于 2016-10-17 20:38:05

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40095130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档