【问题标题】:How do I align the right/left side of a button in android to the center of the screen?如何将android中按钮的右侧/左侧对齐到屏幕中心?
【发布时间】:2015-03-17 00:24:02
【问题描述】:

我在想它会像 android:layout_startOf"centerHoriztonal" 或类似的东西,但我已经搜索过,但找不到任何东西。我适合帮助我尝试制作一个音板应用程序,其中所有按钮可以在多个设备屏幕上具有相同的大小。

【问题讨论】:

  • 你有没有试过???

标签: android button alignment center


【解决方案1】:

试试这个xml

<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:text="Here is button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
/>

<Button
android:id="@+id/button2"
android:text="Here is button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
/>

</RelativeLayout>

【讨论】:

    【解决方案2】:

    您可以将按钮包裹在水平 LinearLayout 中,并使用 layout_weight 将屏幕一分为二,然后 layout_alignParentLeft/layout_alignParentRight 将按钮对齐在占据屏幕宽度一半的 RelativeLayout 内。

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >
    
         <RelativeLayout
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
             <Button 
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
                 android:text="Button Text"
                 /> 
         </RelativeLayout>
    
         <RelativeLayout
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1">
    
         </RelativeLayout>
    
     </LinearLayout>
    

    另一种方法是创建一个在父视图中水平居中的虚拟视图对象,并将您的按钮对齐到它的左侧或右侧:

       <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
          <View
            android:id="@+id/dummyview"
            android:layout_width="0px"
            android:layout_height="0px"
            android:layout_centerHorizontal="true"/>
    
          <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@id/dummyview"
            android:text="Button Text"/>
    
      </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      您确定要在所有设备上使用相同大小的按钮吗?人们试图避免它并使视图元素变得灵活。 无论如何,如果您想设置相同的大小,只需直接在布局中写入

      android:layout_width = "@dimen/valueForButton"; //some absolute value
      

      要从同一点开始,只需在LinearLayout内添加所有按钮并在那里进行一些操作,例如

      android:layout_gravity = "center_horizontal";
      

      你的布局必须是这样的:

      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center_horizontal"
          android:orientation="vertical" >
      
          <Button
              android:id="@+id/button1"
              android:layout_width="@dimen/valueForButton"
              android:layout_height="wrap_content"
              android:text="Button" />
      
          <Button
              android:id="@+id/button2"
              android:layout_width="@dimen/valueForButton"
              android:layout_height="wrap_content"
              android:text="Button" />
      
          <Button
              android:id="@+id/button3"
              android:layout_width="@dimen/valueForButton"
              android:layout_height="wrap_content"
              android:text="Button" />
      </LinearLayout>
      

      【讨论】: