【问题标题】:Android; How to Right Justify Buttons while other widgets stay Left Justified安卓;如何在其他小部件保持左对齐时右对齐按钮
【发布时间】:2019-02-07 14:01:08
【问题描述】:

我是 Android 新手,我的应用终于可以运行了,但是我的布局有问题。

下面的xml 已被剥离,但我标记为<repeats> 我有4 个水平布局。

每个都有不同的小部件SpinnerSeekBarRadioButtons,都由分隔线分隔。

每个人选择一个设置,然后按右侧的Set 按钮。

我的问题,虽然所有小部件都整齐地左对齐,但按钮的垂直对齐到处都是。

如果我只是使用小部件边距,它将不适合不同的 Android 设备。

我怎样才能让Buttons 垂直排列在右侧而不从左侧移动所有其他内容?

           GETTING                              WANT
 -------------------------------   -------------------------------- 
 | <Spinner>  <button>          |  | <Spinner>           <button> |      
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <RadioButton >  <button>     |  | <RadioButton>       <button> |
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <SeekBar>  <button>          |  | <SeekBar>           <button> |
 | < -------- Divider --------> |  | < -------- Divider --------> |
 | <RadioButton>       <button> |  | <RadioButton>       <button> |
 -------------------------------   --------------------------------

这是我的相关代码:

content_main.xml 
 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        tools:scrollY="?android:attr/scrollbarSize">

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

<repeats>   <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <Spinner
                    android:id="@+id/dynamic_TZ_spinner"
                    android:layout_width="223dp"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/dp20"
                    android:layout_marginRight="@dimen/dp20" />
                <Button
                    android:id="@+id/TZ_btnSubmit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/set" />
            </LinearLayout>
            <View
                android:id="@+id/divider3"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_marginTop="@dimen/dp10"
                android:background="?android:attr/listDivider"
                android:visibility="visible" />
....            

            </LinearLayout>
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

【问题讨论】:

    标签: android xml android-layout justify


    【解决方案1】:

    请尝试这种方式,注意当你想把一个小部件放在他们父母的右边或左边时,请使用“android:layout_alignParentRight="true"”或“android:layout_alignParentLeft="true"”

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:gravity="center_vertical"
    tools:scrollY="?android:attr/scrollbarSize">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:orientation="vertical">
    
          <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
           
            <Spinner
                android:id="@+id/dynamic_TZ_spinner"
                android:layout_width="223dp"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_margin="@dimen/dp20" />
            <Button
                android:id="@+id/TZ_btnSubmit"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_margin="@dimen/dp20"
                android:text="@string/set" />
         </RelativeLayout>
         <View
                android:id="@+id/divider3"
                android:layout_width="match_parent"
                android:layout_height="5dp"
                android:layout_marginTop="10dp"
                android:background="?android:attr/listDivider"
                android:visibility="visible" />
            ....
    
        </LinearLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      试试这个:-

      <?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout
      tools:context=".MainActivity"
      tools:showIn="@layout/activity_main">
      
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center_vertical"
          tools:scrollY="?android:attr/scrollbarSize">
      
          <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_alignParentEnd="true"
              android:layout_alignParentTop="true"
              android:orientation="vertical">
      
       <repeats>   <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal">
                  <Spinner
                      android:id="@+id/dynamic_TZ_spinner"
                      android:layout_width="223dp"
                      android:layout_height="wrap_content"
                      android:layout_alignParentLeft="true"
                      android:layout_marginLeft="@dimen/dp20"
                      android:layout_marginRight="@dimen/dp20" />
                  <Button
                      android:id="@+id/TZ_btnSubmit"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_alignParentRight="true"
                      android:text="@string/set" />
              </RelativeLayout>
              <View
                  android:id="@+id/divider3"
                  android:layout_width="match_parent"
                  android:layout_height="5dp"
                  android:layout_marginTop="@dimen/dp10"
                  android:background="?android:attr/listDivider"
                  android:visibility="visible" />
      ....            
      
              </LinearLayout>
      </RelativeLayout>
      

      【讨论】:

      • 感谢您从直系到相对工作的更改
      【解决方案3】:

      您可以为每个小部件和按钮(列)使用线性布局

      【讨论】:

        【解决方案4】:

        您可以使用子级中的FrameLayoutlayout_gravity 属性来实现。

        单行使用:

        <FrameLayout
            android:padding="8dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        
            <RadioButton
                android:id="@+id/radioBtn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="start|center_vertical"
                android:text="Radio Button" />
        
            <Button
                android:id="@+id/btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="end|center_vertical"
                android:text="Button" />
        </FrameLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-01-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-11
          • 2021-08-25
          • 2021-05-10
          相关资源
          最近更新 更多