【问题标题】:android: align a button to the rightmost in XMLandroid:在XML中将按钮对齐到最右边
【发布时间】:2013-03-08 22:42:48
【问题描述】:

我想将按钮对齐到屏幕的最右侧,但我无法做到。 我无法在此处使用 alignParentRight 字段。代码如下:

<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
            android:id="@+id/arrow_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/arrow_left"/>
    <TextView
        android:id="@+id/day_of_the_week"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
            android:id="@+id/arrow_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:
            android:background="@drawable/arrow_right"/>
</LinearLayout>

我该怎么办?我该如何解决这个问题?

【问题讨论】:

  • 如果任何答案帮助您将其标记为答案。

标签: android alignment


【解决方案1】:

使用RelativeLayout 而不是LinearLayout 作为基本布局。然后你就可以实现你想要的。

(即) - android:layout_alignParentRight="true"

【讨论】:

    【解决方案2】:

    假设您想继续使用您的 linearLayout,您是否尝试过添加

     android:gravity="right"
    

    到你的按钮?

    <LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <Button
            android:id="@+id/arrow_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/arrow_left"/>
    <TextView
        android:id="@+id/day_of_the_week"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <Button
            android:id="@+id/arrow_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:background="@drawable/arrow_right"/>
    

    【讨论】:

      【解决方案3】:

      使用Relative layout会这样解决

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
      
          <Button
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"
              android:layout_alignParentRight="true"
              android:text="Button" />
      
      </RelativeLayout>
      

      【讨论】:

        【解决方案4】:
        use relative layout    
        <RelativeLayout 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
               >
                <Button
                        android:id="@+id/arrow_left"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentLeft="true"
                        android:background="@drawable/arrow_left"/>
                <TextView
                    android:id="@+id/day_of_the_week"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    />
                <Button
                        android:id="@+id/arrow_right"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:background="@drawable/arrow_right"/>
            </RelativeLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-02-12
          • 1970-01-01
          • 1970-01-01
          • 2015-11-28
          • 2021-12-20
          • 2021-05-10
          相关资源
          最近更新 更多