我遇到的问题是,当我点击下面xml中显示的其中一个按钮时,按钮上的文字会向下跳一行。因此,如果按钮是1行,则不显示任何文本。如果按钮是2行,则只显示第一行,在按钮的按钮中。
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!得到了想法) - 除了按钮现在显然没有相同的宽度,它看起来很乱。
有没有人知道为什么会这样,以及我如何保留文字,并决定按钮的大小?
答案 0 :(得分: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也可能有用。