【问题标题】:Center a button and textView将按钮和 textView 居中
【发布时间】:2015-12-26 09:30:14
【问题描述】:

有人可以帮我找出我的问题吗?在我桌子的最后一行,我有一个按钮,我希望它居中。下面是编码sn-p。

<TableRow>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_gravity="center"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <Button
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"/>
    </LinearLayout>
</TableRow>
  1. 为什么编码不起作用?
  2. 如何在按钮旁边放一个textView?

谢谢..

【问题讨论】:

    标签: java android center


    【解决方案1】:

    您应该使用android:layout_span="max__number_of_columns" 使RelativeLayout 宽度匹配父级。
    在 RelativeLayout 中,您可以轻松地将 Button 放在中心,将 Textview 放在按钮旁边。
    试试这个代码

    <TableLayout>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2" // 2 is the number of col in your table
             >
             <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="button" />
             <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="aaa"
                android:layout_toRightOf="@id/button"
             />
    
            </RelativeLayout>
        </TableRow>
    </TableLayout>
    

    希望有帮助

    【讨论】:

      【解决方案2】:
      <TableRow
       android:gravity="center_horizontal"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"> 
      
       <LinearLayout android:id="@+id/linearLayout2"   
       android:layout_height="fill_parent" 
       android:layout_gravity="center" 
       android:layout_width="wrap_content">
      
          <Button android:text="Button" android:id="@+id/button1"   
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content">
          </Button>
      
         <TextView 
          ...
          android:layout_width="wrap_content" 
          ...
          />
      
       </LinearLayout>
      
      </TableRow>
      

      【讨论】:

        【解决方案3】:

        此 LinearLayout 布局或其 TableRow 父级可能无用

        试一试:

        <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:gravity="center_horizontal"
                android:text="Button" />
        

        【讨论】:

          【解决方案4】:

          这样做:

          <TableRow>
          <LinearLayout
              android:id="@+id/linearLayout2"
              android:gravity="center_horizontal"
              android:orientation="vertical"
              android:layout_height="wrap_content"
              android:layout_width="match_parent">
          
              <Button
                  android:id="@+id/button1"
                  android:layout_height="wrap_content"
                  android:layout_width="wrap_content"
                  android:text="Button"></Button>
          </LinearLayout>
          

          【讨论】:

            猜你喜欢
            • 2019-05-07
            • 2012-12-25
            • 2018-05-21
            • 2016-09-28
            • 1970-01-01
            • 1970-01-01
            • 2014-08-15
            • 1970-01-01
            • 2016-08-15
            相关资源
            最近更新 更多