Android 如何使ToggleButton和ImageButton在RealtiveLayout中的高度相同?

Android 如何使ToggleButton和ImageButton在RealtiveLayout中的高度相同?,android,layout,button,alignment,togglebutton,Android,Layout,Button,Alignment,Togglebutton,我有一个带有一个ImageButton、一个ToggleButton和一些其他控件的RelativeLayout。两个按钮在相对位置右侧对齐。我希望他们有相同的高度。以下是我的布局xml: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingLeft="5dip" android:paddingRigh

我有一个带有一个
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\u height=“0dip”
并使用
layout\u alignTop
layout\u alignBottom
使ToggleButton与ImageButton具有相同的高度,但这不起作用:

正如您所看到的,ToggleButton未正确对齐,它始终略高于ImageButton。我做错了什么

Android 2.3上的测试更改为

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

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

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

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

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

切换按钮的高度将比图像按钮高一点。您必须增加图像按钮的宽度和高度,使其达到完美效果,使用相同的宽度和高度,您可以发现差异很小

像这样试试

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

切换按钮的高度将比图像按钮高一点。您必须增加图像按钮的宽度和高度,使其达到完美效果,使用相同的宽度和高度,您可以发现差异很小

  • 将水平
    线性布局
    作为
    RelativeLayout
    的子项,并添加
    切换按钮
    ImageView
  • 对于
    ToggleButton
    设置
    android:layout\u height=“match\u 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>
    
    
    
    Edit2:

    我理解你的问题。上面的代码似乎只适用于Android 4.0,默认为Holo主题

    在安卓2.3上,在默认主题中,
    ImageButton
    的样式将背景指定为,其中最上面一行的像素是透明的。而同一主题的
    ToggleButton
    的背景使用的是不透明像素。(单击链接以检查您自己。)

    因此,当放置在
    切换按钮旁边时,它们的高度似乎不相同

  • 将水平
    线性布局
    作为
    RelativeLayout
    的子项,并添加
    切换按钮
    ImageView
  • 对于
    ToggleButton
    设置
    android:layout\u height=“match\u 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>
    
    
    
    Edit2:

    我理解你的问题。上面的代码似乎只适用于Android 4.0,默认为Holo主题

    在安卓2.3上,在默认主题中,
    ImageButton
    的样式将背景指定为,其中最上面一行的像素是透明的。而同一主题的
    ToggleButton
    的背景使用的是不透明像素。(单击链接以检查您自己。)



    因此,当放置在
    切换按钮旁边时,它们似乎没有相同的高度。

    因此,不要使用“包裹内容”。。当您使用它时,按钮将位于其包含的内容的维度中。。所以我建议你自己设置按钮的高度和宽度。你在图形布局编辑器中有这个问题吗?它只在安卓2.3或其他版本上运行吗?@fiddler我的图形布局没有问题(我使用最新的target SDK),安卓4.x也没有问题,我仍然没有在安卓3.x上进行测试,问题似乎只出现在2上。x@Janmejoy没有机会使用自动高度?:(@Andrew为什么不可能不使用“包装内容”…当你使用它时,按钮将在它包含的内容的维度中..因此我建议你自己设置按钮的高度和宽度..你在图形布局编辑器中有这个问题吗?它仅在android 2.3或其他版本上存在吗?@fiddler在我的图形布局中没有问题(我使用最新的target SDK),而且安卓4.x没有问题,我仍然没有在安卓3.x上测试,问题似乎只出现在2上。x@Janmejoy没有机会使用自动高度?:(@Andrew why it’s not possible我用LinearLayout尝试了你的解决方案-没有效果。我用
    android:layout\u height=“match\u parent”
    android:layout\u height进行了测试=“填补家长的空缺"
    onToggleButton@Andrew奇怪的是,我只是尝试了一下,它就成功了。用工作代码编辑了我的答案。你在安卓2.3上测试吗?现在我明白了。有没有办法让它们看起来一样高?也许我可以覆盖主题设置?我用LinearLayout尝试了你的解决方案-没有效果。我用
    安卓进行了测试:layout\u height=“match\u parent”
    android:layout\u height=“fill\u parent”
    onToggleButton@Andrew奇怪的是,我只是尝试了一下,它就成功了。用工作代码编辑了我的答案。你在安卓2.3上测试吗?现在我明白了。有什么办法可以让它们看起来有相同的高度吗?也许我可以覆盖那个主题设置?对我来说不起作用。ToggleButton的视觉高度仍然比ImageB高2像素安卓2.3上的utton对我不起作用。ToggleButton的视觉高度仍然比安卓2.3上的ImageButton高2像素。看来你的解决方案是最合适的。不过,我需要为安卓2.3和安卓4.x提供两种不同的布局。我会等待1-2天,然后接受你的答案。也许其他人会找到b更好的解决方案似乎您的解决方案是最合适的。但是,我需要提供两种不同的布局,适用于Android 2.3和Android 4.x。我将等待1-2天,然后接受您的答案。也许其他人会找到更好的解决方案