如何使用样式和选择器更改按钮填充?
How to change button padding using style and a selector?
我正在尝试调整 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
我知道它正在崩溃,因为无法将 "value" 转换为维度。所以现在我想知道是否有一种方法可以使用选择器和样式来调整按钮的填充,例如按钮上的颜色和可绘制对象?
谢谢。
尝试将selector包裹在layer-list里面,可以看看代码here。
试试这个
我这里使用的是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 class.
的子 class,我能够实现我想要的
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 按钮来覆盖标准按钮的行为,允许文本在按下按钮时向下 "push" 并在按下按钮时向上抬起按钮释放。
<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>
我正在尝试调整 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
我知道它正在崩溃,因为无法将 "value" 转换为维度。所以现在我想知道是否有一种方法可以使用选择器和样式来调整按钮的填充,例如按钮上的颜色和可绘制对象?
谢谢。
尝试将selector包裹在layer-list里面,可以看看代码here。
试试这个
我这里使用的是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 class.
的子 class,我能够实现我想要的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 按钮来覆盖标准按钮的行为,允许文本在按下按钮时向下 "push" 并在按下按钮时向上抬起按钮释放。
<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>