【问题标题】:How to change button padding using style and a selector?如何使用样式和选择器更改按钮填充?
【发布时间】:2019-01-16 13:23:33
【问题描述】:

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

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

对于文本颜色,我可以使用 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

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

谢谢。

【问题讨论】:

    标签: android button styles selector padding


    【解决方案1】:

    尝试把选择器包裹在layer-list里面,可以看一下代码here

    【讨论】:

    • 我认为这不适用于我想要完成的任务,因为它似乎为每个状态提供了相同的填充。我正在尝试将填充更改为按下状态的不同值和默认状态的不同值。谢谢。
    【解决方案2】:

    试试这个

    这里使用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:background 使用 9-patch,我尝试调整您的方法,但在按钮的按下状态下看不到文本向下移动。谢谢。
    • @kralvarado 您是否尝试过删除您的风格中的paddingToppaddingBottom 属性。?
    • 是的,我确实尝试过,结果相同。
    【解决方案3】:

    好的,所以我找不到使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-16
      • 2015-05-09
      • 1970-01-01
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      相关资源
      最近更新 更多