android - 如何使用 LinearLayout 定位 Button 死点

标签 android button android-linearlayout center

我正在尝试将位于 TextView 下方的按钮设置为屏幕的死点。
目前,我只让它们成为水平居中,而不是垂直居中,所以 Buttons 就在 TextView 的下面,这不是我想要的。
我什至尝试过使用 layout_gravity 而不是 gravity,但仍然没有成功。

这是我得到的

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView android:text="CALCULATOR"
    android:id="@+id/appTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

<Button android:text="BASIC MATH"
    android:id="@+id/basicMath"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center"
    />

<Button android:text="BASIC ALGEBRA"
    android:id="@+id/alg"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center"
    />

<Button android:text="BASIC CALCULUS"
    android:id="@+id/calc"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center"
    />

<Button android:text="INFO"
    android:id="@+id/info"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal|center"
    />
    </LinearLayout>

最佳答案

我真的受不了嵌套布局,抱歉。
因此,我真的不得不提出替代方案。

<RelativeLayout
    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"
    tools:context=".MainActivity"
    >
    <TextView
        android:text="CALCULATOR"
        android:id="@+id/appTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
    />
    <!-- All you need is a little trick -->
    <!-- This dummy View is invisible, but it's CENTERED -->
    <!-- The 4 Buttons will be positioned in RELATION to this one -->
    <!-- This is the power of RelativeLayouts -->
    <View
        android:id="@+id/vwDummy"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerInParent"true"
    />
    <!-- These 2 are horizontally centered and go above the dummy -->
    <Button
        android:text="BASIC ALGEBRA"
        android:id="@+id/alg"
        android:layout_above="@id/vwDummy"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />
    <Button
        android:text="BASIC MATH"
        android:id="@+id/basicMath"
        android:layout_above="@id/alg"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />
    <!-- These 2 are horizontally centered and go below the dummy -->
    <Button
        android:text="BASIC CALCULUS"
        android:id="@+id/calc"
        android:layout_below="@id/vwDummy"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />
    <Button
        android:text="INFO"
        android:id="@+id/info"
        android:layout_below="@id/calc"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
    />
</RelativeLayout>

如评论中所示,我利用了 RelativeLayout 子项的相对性
您只需要一个小技巧:

  • 在中心创建一个不可见的虚拟 View 。
  • 将 4 个按钮与此按钮对齐。

关于android - 如何使用 LinearLayout 定位 Button 死点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31770460/

相关文章:

使用蓝牙时,Android 音乐在 sleep 模式下跳过

android - react-native 中的用户角色明智的抽屉导航

android - Android 解包错误

android - 单击父 View 时调用 subview onClickListener

android - 在线性布局中将七个按钮并排放置

java - Android模拟器中的问题写入文件问题

android - 你如何制作这些按钮?

java - 按住按钮进行重复操作——是否有 android :onClick for holds?(在 XML 文件中)

java - 使用 selenium webdriver 选择日历按钮

android - ScrollView 滚动,但不完全显示其内容