Android按钮文本移动

Android按钮文本移动,android,layout,button,text,Android,Layout,Button,Text,我遇到的问题是,当我单击下面xml中显示的一个按钮时,按钮上的文本会跳下一行。因此,如果按钮为1行,则不显示文本。如果按钮为2行,则在按钮的按钮中仅显示第一行 onClick方法中的任何内容都不会更改布局 这里是XML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:l

我遇到的问题是,当我单击下面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>

.. 一些其他的布局和控件可以很好地工作
.. 一些其他的布局和控件可以很好地工作
现在,如果我改变按钮的布局宽度来包装内容,一切都很好(这是我的想法)-除了按钮现在明显没有相同的宽度,而且看起来很凌乱


有人知道为什么会发生这种情况,以及我如何既保留文本,又决定按钮的大小吗?

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

向表格行添加线性布局,并使用权重控制按钮的精确布局。大概是这样的:

<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>

注意事项:

  • 可能会给性能带来冲击
  • 根据记忆输入,请原谅任何打字错误
  • 未经测试
  • 使用填充/边距调整精确尺寸

相对论也可能有效。

完全同意这一点。你想尽量避免硬编码的视图尺寸,例如android:layout\u width=“120dp”对此我有几个问题。第一:你的建议是线性布局,在我已经得到的桌面视图中?如果带有“weigthsum”行的线性布局可以将按钮调整为彼此相邻,而不是在下方-这不会破坏表格布局的重点吗?第二:如果我使用填充/边距来调整按钮的大小,如果我更改按钮上的文本,按钮的大小会不会改变?我希望按钮彼此大小相同,并且无论发生什么情况都保持该大小。(当然,如果文本变长,它会变为第二行,即换行符的内容高度,但这很好。)如果您希望按钮相互下方,对不起,我错过了这个要求,这会变得更复杂,特别是如果您在代码中更改文本。请确认这是你想要做的。你一定是误解了;我希望按钮挨着,在同一行上。