如何使用样式和选择器更改按钮填充?



我正在尝试调整按钮中的填充,以便当用户按下按钮时,文本向下移动以帮助直观地指示已按下。

样式.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Img_Button">
    <item name="android:layout_width">@dimen/img_button_width</item>
    <item name="android:layout_height">@dimen/img_button_height</item>
    <item name="android:layout_marginTop">@dimen/img_button_marginTop</item>
    <item name="android:layout_marginBottom">@dimen/img_button_marginTop</item>
    <item name="android:paddingTop">@drawable/button_padtop_states</item>
    <item name="android:paddingBottom">@drawable/button_padbottom_states</item>
    <item name="android:background">@drawable/button_img_states</item>
    <item name="android:gravity">center</item>
    <item name="android:textColor">@drawable/button_txt_states</item>
    <item name="android:textSize">@dimen/img_button_text_size</item>
    <item name="android:textStyle">bold</item>
</style>

对于文本颜色,我可以使用样式.xml文件中指示的选择器更改颜色

button_txt_states.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@color/green_med" />
    <item android:state_pressed="true" android:color="@color/white" />
    <item android:state_focused="true" android:color="@color/white" />
    <item android:color="@color/white" />
</selector>

因此,这些会根据按钮的状态更改按钮中的文本颜色。 因此,我想完成的是分配不同的填充值,当用户按下按钮以直观地指示按钮按下时,文本会向下移动一点。我已经为 style.xml 文件中的 paddingTop 和 paddingBottom 值创建了一个选择器文件。

button_padtop_states.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:value="2dp" />
    <item android:state_pressed="true" android:value="3dp" />
    <item android:state_focused="true" android:value="2dp" />
    <item android:value="2dp" />
</selector>

所以一切都可以编译,我可以在设备上启动应用程序,但崩溃

E/AndroidRuntime(19515): Caused by: java.lang.UnsupportedOperationException:
Can't convert to dimension: type=0x3

我知道它崩溃了,因为没有办法将"值"转换为维度。所以现在我想知道是否有办法使用选择器和样式来调整按钮的填充,例如按钮上的颜色和可绘制对象?

谢谢。

尝试将选择器包装在层列表中,您可以在此处查看代码。

试试这个

在这里使用AppCompatRadioButton您可以根据自己的观点进行更改。

在样式中添加此值

<item name="android:background">@drawable/radiobtn_background_state</item> 

radiobtn_background_state.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radiobtn_checked" android:state_checked="true" />
<item android:drawable="@drawable/radiobtn_normal" />
</selector>

radiobtn_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/colorGreyedOut" >
</solid>
</shape>

radiobtn_checked.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="@color/colorAccent" >
</solid>
<padding android:top="10dp" android:left="0dp" android:right="0dp" android:bottom="0dp" />  /* Add your padding here */
</shape>

新增:在后台应用此内边距时,从样式中删除内边距

好的,所以我找不到使用 Android 的 XML 文件为不同按钮状态设置不同填充值的方法。 我能够通过创建一个 android.widget.Button 类的子类来实现我想要的。

public class PushButton extends Button {
    private int dropPixels;
    public PushButton( Context context ) {
        super( context );
        init();
    }
    public PushButton( Context context, AttributeSet attrs ) {
        super( context, attrs );
        init();
    }
    public PushButton( Context context, AttributeSet attrs, int defStyleAttr ) {
        super( context, attrs, defStyleAttr );
        init();
    }
    public PushButton( Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes ) {
        super( context, attrs, defStyleAttr, defStyleRes );
        init();
    }
    private void init() {
        dropPixels = getContext().getResources().getDimensionPixelSize( R.dimen.pushbutton_drop_padding );
    }
    @Override
    public boolean onTouchEvent( MotionEvent event ) {
        super.onTouchEvent( event );
        switch ( event.getAction() ) {
        case MotionEvent.ACTION_DOWN:
            setPadding( getPaddingLeft(), dropPixels, getPaddingRight(), 0 );
            break;
        case MotionEvent.ACTION_UP:
            setPadding( getPaddingLeft(), 0, getPaddingRight(), 0 );
            break;
        }
        invalidate();
        return true;
    }
}

在我的 XML 文件中,我将使用 PushButton Button 覆盖标准按钮行为的行为,允许文本在按下按钮时向下"推动",并在按钮释放时重新抬起。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="horizontal" >
    <com.testapp.PushButton
        android:id="@+id/start"
        style="@style/Push_Button"
        android:enabled="true"
        android:text="@string/start_button_text" />
    <com.testapp.PushButton
        android:id="@+id/next"
        style="@style/Push_Button"
        android:enabled="true"
        android:text="@string/next_button_text" />
</LinearLayout>

最新更新