【问题标题】:Android layout: center and align to the bottom text viewAndroid 布局:居中并与底部文本视图对齐
【发布时间】:2017-01-19 08:01:09
【问题描述】:

我需要将重置按钮与底部对齐并居中。这是布局:

这是我的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team A" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />

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

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

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

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team B" />

        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />

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

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

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addOneForTeamB"
            android:text="Free throw" />


    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/linear1"
            android:layout_margin="8dp"
            android:onClick="ResetTeamAandB"
            android:text="Reset" />
    </RelativeLayout>

</LinearLayout>

问题是我的父布局在horizontal 方向上是linear,它在第三列中添加了button。这就是为什么我试图将它嵌套在 relative 布局中并以某种方式对齐它。任何想法将不胜感激:)

【问题讨论】:

    标签: android xml android-linearlayout centering


    【解决方案1】:

    给你

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="Team A" />
    
                <TextView
                    android:id="@+id/team_a_score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="0" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addThreeForTeamA"
                    android:text="+3 Points" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addTwoForTeamA"
                    android:text="+2 Points" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addOneForTeamA"
                    android:text="Free throw" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="Team B" />
    
                <TextView
                    android:id="@+id/team_b_score"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="0" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addThreeForTeamB"
                    android:text="+3 Points" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addTwoForTeamB"
                    android:text="+2 Points" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="8dp"
                    android:onClick="addOneForTeamB"
                    android:text="Free throw" />
    
    
            </LinearLayout>
    
        </LinearLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></View>
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:layout_gravity="center_horizontal"
            android:onClick="ResetTeamAandB"
            android:text="Reset" />
    
    </LinearLayout>
    

    【讨论】:

    • 感谢您的快速回复。它对我有用!但是我想知道这是否是添加空视图的好习惯?可以有任何其他方式来实现它吗?只是问,因为我是 Android 的新手:)
    • 在这种情况下,你必须在线性布局中使用View,这没有错。高兴它解决了你的问题:)
    • 到处使用权重并不是一个好习惯。 UI 需要计算每个视图,这会增加额外的负载。
    【解决方案2】:

    如果我明白您在寻找什么,这应该可以解决问题!这将保留三列并将“重置”按钮放在第二列的底部。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team A" />
    
        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addThreeForTeamA"
            android:text="+3 Points" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addTwoForTeamA"
            android:text="+2 Points" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addOneForTeamA"
            android:text="Free throw" />
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Team B" />
    
        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="0" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addThreeForTeamB"
            android:text="+3 Points" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addTwoForTeamB"
            android:text="+2 Points" />
    
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            android:onClick="addOneForTeamB"
            android:text="Free throw" />
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:onClick="ResetTeamAandB"
                android:text="Reset"
                android:layout_marginTop="19dp"
                android:layout_alignParentTop="false"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="false"
                android:layout_alignParentBottom="true" />
        </RelativeLayout>
    
    
    </LinearLayout>
    
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </LinearLayout>
    

    【讨论】:

    • 感谢您。但是我不需要 3 列,而是 2 列,并且在最底部和居中重置
    【解决方案3】:

    为单独的视图添加额外的 LinearLayout。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/linear1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team A" />
    
            <TextView
                android:id="@+id/team_a_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addThreeForTeamA"
                android:text="+3 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addTwoForTeamA"
                android:text="+2 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addOneForTeamA"
                android:text="Free throw" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="Team B" />
    
            <TextView
                android:id="@+id/team_b_score"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:padding="4dp"
                android:text="0" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addThreeForTeamB"
                android:text="+3 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addTwoForTeamB"
                android:text="+2 Points" />
    
            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:onClick="addOneForTeamB"
                android:text="Free throw" />
    
    
        </LinearLayout>
        </LinearLayout>
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|bottom"
            >
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_margin="8dp"
                android:onClick="ResetTeamAandB"
                android:text="Reset" />
        </RelativeLayout>
    
        </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      我想出了一个替代解决方案,并且效果很好。我将所有内容都包装在相对布局中,因此我的水平线性布局位于顶部,然后按钮位于相对布局的底部。这是xml:

      <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">
      
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="horizontal">
      
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="vertical">
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:padding="4dp"
                  android:text="Team A" />
      
              <TextView
                  android:id="@+id/team_a_score"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:padding="4dp"
                  android:text="0" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addThreeForTeamA"
                  android:text="+3 Points" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addTwoForTeamA"
                  android:text="+2 Points" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addOneForTeamA"
                  android:text="Free throw" />
          </LinearLayout>
      
          <LinearLayout
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:orientation="vertical">
      
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:padding="4dp"
                  android:text="Team B" />
      
              <TextView
                  android:id="@+id/team_b_score"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:padding="4dp"
                  android:text="0" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addThreeForTeamB"
                  android:text="+3 Points" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addTwoForTeamB"
                  android:text="+2 Points" />
      
              <Button
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_margin="8dp"
                  android:onClick="addOneForTeamB"
                  android:text="Free throw" />
      
          </LinearLayout>
      
      </LinearLayout>
      
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_margin="8dp"
          android:onClick="ResetTeamAandB"
          android:text="Reset" />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-16
        • 2016-11-11
        • 1970-01-01
        相关资源
        最近更新 更多