对齐drawableLeft与按钮的文字按钮、文字、drawableLeft

2023-09-05 05:22:44 作者:耂资ヘ专情限量版

下面是我的布局:

我现在面临的问题是与绘制对号。我怎么会去调整它旁边的文字,按钮内他们两人为中心?下面是XML:

The issue I'm facing is with the drawable checkmark. How would I go about aligning it next to the text, both of them centered within the button? Here is the XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PostAssignmentActivity" >

    <LinearLayout
        style="?android:attr/buttonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableLeft="@drawable/ic_checkmark_holo_light"
            android:text="Post" />

        <Button
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Cancel" />
    </LinearLayout>

</RelativeLayout>

应用安卓重力=center_vertical拉文与可绘制在一起,但随后的文本将不再在中心对齐

Applying android:gravity="center_vertical" pulls the text and drawable together, but then the text is no longer aligned in the center.

推荐答案

设置安卓以下属性来您的第一个按钮里面。这将迫使 drawableLeft 以下属性来金额的权利。这是快速/哈克的解决方案。

Solution 1

Set android:paddingLeft inside your first button. This will force the drawableLeft by paddingLeft amount to the right. This is the fast/hacky solution.

,使用包含一个TextView和ImageView的一个LinearLayout中。这是一个更好的解决方案。它给你的对号的定位更多的灵活性。

Instead of using a ButtonView, use a LinearLayout that contains both a textview and imageview. This is a better solution. It gives you more flexibility in the positioning of the checkmark.

替换您ButtonView具有以下code。您需要的LinearLayout 的TextView 使用 buttonBarButtonStyle 使背景颜色上的选择正确,文字大小是正确的。您需要设置机器人:背景=#0000的孩子,所以只有LinearLayout中处理的背景颜色

Replace your ButtonView with the following code. You need the LinearLayout and TextView to use buttonBarButtonStyle so that the background colors are correct on selection and the text size is correct. You need to set android:background="#0000" for the children, so that only the LinearLayout handles the background coloring.

<LinearLayout
    style="?android:attr/buttonBarButtonStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" >
    <ImageView 
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:background="#0000"
        android:src="@drawable/ic_checkmark_holo_light"/>
    <TextView
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:clickable="false"
        android:background="#0000"
        android:text="Done" />
</LinearLayout>

下面是一些截图我把在尝试了这一点。

Here are some screenshots I took while trying this out.