android - 为什么我的 "centered"对象在我的 RelativeLayout 中稍微偏离中心?

标签 android android-layout android-relativelayout gravity

我想制作两个大小相同的单选按钮,带有自定义背景、文本和文本右侧的图像。由于它们与标准“按钮”有何不同,我使用可点击的“RelativeLayout”制作它们。

文本和图像的高度不同,但我希望每一个都在按钮中垂直居中。我还希望文本+图像的组合在按钮中水平居中。第二部分是我遇到的麻烦;它偏离中心,靠近左侧。在下图中,左侧是我想要的,而右侧是正在发生的情况。其中一个按钮(文本较长的按钮)上的图像也被调整为更小...尽管按钮右侧仍有足够的空间。

What I want is on the left, and what's happening is on the right.

这是我的代码:

<LinearLayout
    android:baselineAligned="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <RelativeLayout
        android:id="@+id/my_button"
        android:background="@drawable/radio_button"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal">
        <TextView
            android:id="@+id/button_textview"
            android:text="@string/button_label"
            android:textAppearance="?android:attr/textAppearanceButton"
            android:layout_centerVertical="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"></TextView>
        <ImageView
            android:contentDescription="@string/image_description"
            android:id="@+id/button_imageview"
            android:layout_centerVertical="true"
            android:src="@drawable/my_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/button_textview">
        </ImageView>
    </RelativeLayout>

    <RelativeLayout
        ... same thing for the second button ...
    </RelativeLayout>

</LinearLayout>

我做错了什么?

最佳答案

将其用作您的按钮:

<LinearLayout
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:orientation="horizontal"
              android:layout_gravity="center_horizontal"
              >
    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center_vertical"
              android:text="MyButton"/>
    <ImageView android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_gravity="center_vertical"
               android:src="@drawable/my_image"/>

</LinearLayout>

现在,您可以将其放置在其他父 View 中。要将布局属性应用于上述按钮,请将这些属性放在外部 <LinearLayout> 中上面按钮的标签。

备选方案:

您可以设置要在 TextView 的侧面(左、右、上、下)绘制的自定义图像使用如下属性: android:drawableLeft="@drawable/my_image"

关于android - 为什么我的 "centered"对象在我的 RelativeLayout 中稍微偏离中心?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204097/

相关文章:

android - 支持从右到左的语言

android - altbeacon 以 10Hz 连续检测信标 RSSI 值

android - 安卓中的VSYNC是什么

android - 在 Android 中使用预览绘制圆形 GeoFence

android - 我怎样才能使 RelativeLayout 适合屏幕?

java - 如何将 ArrayList 转换为最终的 String 数组?

java - Android改造设计模式

android - 如何创建固定/动态宽度的列布局?

android - 如何在相对布局中的微调器旁边显示标签

Android:RelativeLayout 中的重力 ImageView 失败