【问题标题】:Buttons are not in same size按钮大小不同
【发布时间】:2013-10-21 10:36:25
【问题描述】:

我创建了一个带有“相同大小”按钮的应用程序,并向其中添加了同样大小的图像。以下是我的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".HomeScreen" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stretchColumns="*"
        android:weightSum="4" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/fBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/fStr" />

            <Button
                android:id="@+id/rBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/rStr" />

            <Button
                android:id="@+id/sBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/sStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/cBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/cStr" />

            <Button
                android:id="@+id/aBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/aStr" />

            <Button
                android:id="@+id/lBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/lStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/oBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/oStr" />

            <Button
                android:id="@+id/tBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/tStr" />

            <Button
                android:id="@+id/eBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/eStr" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1" >

            <Button
                android:id="@+id/dBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/dStr" />

            <Button
                android:id="@+id/hBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/hStr" />

            <Button
                android:id="@+id/aBtn"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:drawableTop="@drawable/ic_launcher"
                android:layout_weight=".3"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="@string/aStr" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

但问题是,它并没有产生我所期望的。以下是它在 Eclipse 编辑器中的显示方式

以下是它在模拟器中的显示方式

如您所见,第一列和第三列比第二列大。但我期望的是相同大小的列。

以下是我的strings.xml 文件,以防万一

<?xml version="1.0" encoding="utf-8"?>
<resources>


<string name="fStr">Fffffffff</string>
<string name="rStr">Rrrrrr</string>
<string name="sStr">Ssssss       </string>
<string name="cStr">Ccccccc</string>
<string name="aStr">Aaaaaaa</string>
<string name="lStr">Lllll</string>
<string name="oStr">Oooooooooooo</string>
<string name="tStr">Ttttt</string>
<string name="eStr">Eeeeee</string>
<string name="dStr">Dddddddddd</string>
<string name="cStr">Cccccccc</string>
<string name="hStr">Hhhh</string>
<string name="aStr">Aaaaaaaaaa aaaa</string>

</resources>

我该如何解决这个问题?

【问题讨论】:

  • 对于您正在使用 weight 属性和 widht="wrap_content" 的按钮。尝试使用 layout_width="0dip" 而不是 wrap_content

标签: java android eclipse button user-interface


【解决方案1】:

尝试将layout_width 设置为 0dp,这已经为我解决了这个问题。

【讨论】:

  • 是的,将其设置为 0dp0dip 应该使它使用权重。否则,您将覆盖 weight 属性。 +1
  • 你的意思是“layout_width”?
  • 感谢您的回复。但是布局宽度是多少呢?
  • 应用了权重的任何元素的宽度。
  • 是的,就是这样。谢谢你!
【解决方案2】:

将所有Buttonandroid:layout_width改为fill_parent

android:layout_width="fill_parent"

【讨论】:

    【解决方案3】:

    试试这个方法:

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Buttonssssssssssssssssssssssss" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Buttonssssssssssssssssssssssss" />
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Buttonssssssssssssssssssssssss" />
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      使用填充父级更改表格行的布局:宽度属性

       android:layout_width="fill_parent"
      

      【讨论】:

        【解决方案5】:

        我认为你可以解决这个问题添加

        <LinearLayout
        android:layout_width="fill_parent"
        android:orientation="horizontal"
        

        在您的 LinearLayout(here) 中,您可以添加您的块:android:layout_width="0.0dip" 如果你想在块之间放置一些边距......使用:android:layout_marginLeft="sizehere.0dip"android:layout_marginRight="sizehere.0dip"

        </LinearLayout>
        

        【讨论】:

          【解决方案6】:

          我同意并推荐使用layout_weight,您必须在view 对象之间进行分布或划分宽度。

          另外查看您的代码,我建议您在使用 TableLayout 时使用较少的视图,如果您只使用带有三个按钮的 LinearLayout 并正确使用上述属性,则实际上不需要。

          我也不确定为什么在这种情况下你实际上需要RelativeLayout。如果您使用 android-sdk 中的层次结构查看器工具,那么您将知道您使用的布局的视图数量越少,您的屏幕将使用的内存越低,因此响应速度越快。

          后一个只是一个建议

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-08-19
            • 1970-01-01
            • 2011-02-26
            • 2018-05-31
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多