【问题标题】:How to make ToggleButton and ImageButton to be the same height in RealtiveLayout?如何使 RealtiveLayout 中的 ToggleButton 和 ImageButton 高度相同?
【发布时间】:2013-01-01 22:54:06
【问题描述】:

我有一个RelativeLayout,一个ImageButton,一个ToggleButton 和其他一些控件。两个按钮都在 RelativeLayout 的右侧对齐。我希望它们具有相同的高度。这是我的布局 xml:

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingBottom="10dip" >

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/copy"
        android:layout_alignParentRight="true" />

    <ToggleButton
        android:id="@+id/buttonView"
        android:layout_width="wrap_content"
        android:layout_height="0dip"
        android:textOn="Hide"
        android:textOff="View" 
        android:layout_toLeftOf="@id/imageButton" 
        android:layout_alignTop="@id/imageButton"
        android:layout_alignBottom="@id/imageButton" />

    <!-- ... other controls ... -->
</RelativeLayout>

我尝试设置layout_height="0dip" 并使用layout_alignToplayout_alignBottom 使ToggleButton 与ImageButton 具有相同的高度,但这不起作用:

如您所见,ToggleButton 对齐不正确,它总是略高于 ImageButton。我做错了什么?

在 Android 2.3 上测试

【问题讨论】:

  • 所以不要使用“wrap_content”..当你使用它时,按钮将在他包含的内容的尺寸中..所以我建议你设置按钮的高度和自己的宽度..
  • 您在图形布局编辑器中有这个问题吗?是只在 android 2.3 上还是在其他版本上?
  • @fiddler 我的图形布局没有问题(我使用最新的目标 SDK),在 Android 4.x 中没有问题,我仍然没有在 Android 3.x 上测试,问题似乎只出现在 2.x
  • @Janmejoy 没有机会使用自动高度? :(
  • @Andrew 为什么不可能

标签: android layout button alignment togglebutton


【解决方案1】:

像这样试试..

         <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dip"
        android:paddingLeft="5dip"
        android:paddingRight="5dip" >

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="55dp"
        android:layout_height="55dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:src="@drawable/ic_launcher" />

    <ToggleButton
        android:id="@+id/buttonView"
        android:layout_width="55dp"
        android:layout_height="54dp"
        android:layout_alignTop="@id/imageButton"
        android:layout_toLeftOf="@+id/imageButton"
        android:textOff="View"
        android:textOn="Hide" />

    <!-- ... other controls ... -->
</RelativeLayout>

切换按钮的高度会比图像按钮高一点,你必须增加图像按钮的宽度和高度,这样才能完美,使用相同的宽度和高度,你会发现差异很小

【讨论】:

  • 看来您的解决方案是最合适的。但是,我需要为 Android 2.3 和 Android 4.x 提供 2 种不同的布局。我会等待 1-2 天,然后会接受你的回答。也许其他人会找到更好的解决方案
【解决方案2】:

改成

<ImageButton
    android:id="@+id/imageButton"
    android:layout_height="150dp"//some height
    ...

<ToggleButton
    android:id="@+id/buttonView"
    android:layout_height="150dp"//same height as above

【讨论】:

  • 不适合我。在 Android 2.3 上,ToggleButton 的视觉高度仍然比 ImageButton 多 2 个像素
【解决方案3】:
  1. 有一个水平的LinearLayout 作为RelativeLayout 的子级并添加ToggleButtonImageView 给它。
  2. 对于ToggleButton 设置android:layout_height="match_parent"

编辑:
这是工作代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="5dip"
    android:paddingRight="5dip"
    android:paddingBottom="10dip" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ToggleButton
            android:id="@+id/buttonView"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:textOff="View"
            android:textOn="Hide" />

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:src="@drawable/copy" />
    </LinearLayout>
</RelativeLayout>

编辑2:

了解您的问题。上面的代码似乎仅适用于 Android 4.0,默认 Holo 主题。

在 Android 2.3 上,默认主题中,ImageButton 的样式指定背景为btn_default_normal,其中最上面一行像素是透明的。而同一主题的ToggleButton 的背景使用btn_default_small_normal,其顶行有不透明像素。 (点击链接自行检查。)

因此,当放置在 ToggleButton 旁边时,它们似乎具有不同的高度。

【讨论】:

  • 我用 LinearLayout 尝试了你的解决方案 - 没有效果。我在 ToggleButton 上使用 android:layout_height="match_parent"android:layout_height="fill_parent" 进行了测试
  • @Andrew Strange,我刚试了一下,效果很好。用工作代码编辑了我的答案。
  • 您在 Android 2.3 上测试?
  • 现在我明白了。任何想法如何使它们看起来具有相同的高度?也许我可以以某种方式覆盖该主题设置?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
  • 2014-09-05
  • 1970-01-01
  • 2019-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多