2012-11-07 75 views
0

我得到的问题是,当我点击下面的xml中显示的按钮之一,按钮上的文本跳下来一行。所以如果按钮是1行,则不显示文本。如果按钮是2行,则只显示第一行,位于按钮的按钮中。Android按钮文本移动

onClick方法中的任何内容都不会更改布局。

继承人的XML

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:id="@+id/layout_main"> 


    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:orientation="vertical" > 

.. a few other layouts and controls that works fine 



     <TableLayout 
       android:id="@+id/tableLayout276" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <TableRow 
        android:id="@+id/TableRow10" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="left" 
        android:gravity="center" 
        android:orientation="vertical" > 



        <Button 
         android:id="@+id/BtnRet" 
         android:layout_width="120dp" 
         android:layout_height="wrap_content" 
         android:text="Ret" 
         android:gravity="center" 
         android:textSize="18dp" />  

        <Button 
         android:id="@+id/BtnVisningsType" 
         android:layout_width="120dp" 
         android:layout_height="wrap_content" 
         android:text="Decimaltal" 
         android:gravity="center" 
         android:textSize="18dp" /> 

        <Button 
         android:id="@+id/BtnFunktioner" 
         android:layout_width="120dp" 
         android:layout_height="wrap_content" 
         android:text="Funktioner" 
         android:gravity="center" 
         android:textSize="18dp" />  
      </TableRow> 
     </TableLayout> 

.. a few other layouts and controls that works fine 


</LinearLayout> 
</ScrollView> 
</LinearLayout> 

现在,如果我改变按钮上的layout_width为wrap_content,这一切工作正常(接到When changing textview, the text on a button below the textview moves down. Help!的想法) - 除了按钮现在obviusly不具有相同的宽度,它看起来很凌乱。

有没有人有任何想法为什么会发生这种情况,以及我如何保持文本,并决定按钮的大小?

回答

2

这增加了布局的复杂性,但可能是一个解决方案。

将LinearLayout添加到您的表格行,并使用权重来控制按钮的精确布局。事情是这样的:

<TableRow 
       android:id="@+id/TableRow10" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:gravity="center" 
       android:orientation="vertical" > 

    <LinearLayout android:layout_height="match_parent" 
        android:layout_width="match_parent" 
        android:weightSum="3" 
        android:orientation="horizantal"/> 

       <Button 
        android:id="@+id/BtnRet" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:text="Ret" 
        android:gravity="center" 
        android:textSize="18dp" />  

       <Button 
        android:id="@+id/BtnVisningsType" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:text="Decimaltal" 
        android:gravity="center" 
        android:textSize="18dp" /> 

       <Button 
        android:id="@+id/BtnFunktioner" 
        android:layout_weight="1" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:text="Funktioner" 
        android:gravity="center" 
        android:textSize="18dp" />  

    </LinearLayout> 

</TableRow> 

注意事项:

  • 可能会给性能命中
  • 从内存类型的,请原谅任何错字
  • 没有测试
  • 使用填充/保证金调整精确上浆

RelativeLayout migh吨也工作。

+0

完全同意这一点。您想尝试并避免硬编码的视图尺寸,例如android:layout_width =“120dp” –

+0

我对此有几个问题。第一:你的建议是一个线性布局,在tableview里面已经有了吗?如果线条“weigthsum”的线性布局可以将按钮调整为彼此相邻而不是下 - 不会破坏tablelayout的点吗? – user1285334

+0

第二:如果我使用填充/边距来调整按钮的大小,将不会改变按钮的大小,如果我改变它的文字?我希望按钮的尺寸与其他尺寸相同,并且无论发生什么情况,都要保持这种尺寸。 (当然,如果文字变长了,它会变成wrap_content高度的第二行,但那很好。) – user1285334