删除自定义切换按钮文本空间android

时间:2015-03-14 00:13:34

标签: android togglebutton android-togglebutton

为什么我的切换按钮的宽度错误并且有文本空间?

enter image description here

我希望此切换按钮与其他按钮的宽度完全相同

以下是代码:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#242424"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/ten_dp"
            android:layout_weight="30"
            android:background="@drawable/action_bar_btn_bg"
            android:text="Close"
            android:textColor="@color/abc_primary_text_material_dark" />


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:gravity="right"
            android:orientation="horizontal"
            android:padding="@dimen/ten_dp" >

            <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/five_dp"
                android:background="@drawable/action_bar_btn_bg"
                android:button="@drawable/btn_fav"
                android:gravity="center"
                android:textOn="@null"
                android:textOff="@null" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/five_dp"
                android:background="@drawable/action_bar_btn_bg"
                android:src="@drawable/ic_action_left" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/action_bar_btn_bg"
                android:src="@drawable/ic_action_right" />
        </LinearLayout>
    </LinearLayout>

@绘制/ action_bar_btn_bg

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_normal"/>

@绘制/ btn_fav

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/ic_action_star_normal" android:state_checked="false" />
<item android:drawable="@drawable/ic_action_star_active" android:state_checked="true"/>

请帮帮我,告诉我哪里出错了? 谢谢。

1 个答案:

答案 0 :(得分:0)

尝试在线性布局中添加权重和标记

    android:weightSum = "3"

然后将权重属性添加到切换和图像按钮

    android:weight="1"

编辑:因为你想把图像置于toggleButton中心, 我建议另外尝试这个:

<ToggleButton
    android:id="@+id/YourID
    android:layout_width="0"
    android:layout_height="75dp"
    android:layout_weight="1"
    android:checked="false"
    android:textOn=""
    android:textOff=""
    android:drawableTop="@drawable/yourImage"
    android:layout_gravity="center"
    android:textColor="#yourColor"