Android的按钮样式背景,XML [英] Android Button Style Background with XML
问题描述
所以,我有两个按钮样式pretty的接近我想要的,但我需要一些帮助得到它所有的方式。我现在已经是第一个屏幕上放映,我要的是第二个。任何人有什么想法?我已经附加了相关的code。
布局的xml:
....
<按钮
机器人:ID =@ + ID / sched_button
风格=机器人:ATTR / buttonBarButtonStyle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID /同步
机器人:layout_alignParentTop =真
机器人:背景=@可绘制/ round_left_corner_active
机器人:文本=@字符串/ headersched
机器人:layout_toLeftOf =@ + ID /占位符
机器人:paddingRight =@扪/ scheduleheadermargin
机器人:以下属性来=@扪/ scheduleheadermargin/>
<按钮
机器人:ID =@ + ID / sched_fav_button
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID / sched_button
机器人:layout_alignParentTop =真
机器人:layout_toRightOf =@ + ID /占位符
机器人:背景=@可绘制/ round_right_corner
机器人:以下属性来=@扪/ scheduleheadermargin
机器人:paddingRight =@扪/ scheduleheadermargin
机器人:文本=@字符串/ headerfav/>
...
round_left_corner_active:
< XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目>
<形机器人:形状=矩形>
[固体机器人:颜色=@色/主动/>
<填充
机器人:底部=2DP
机器人:左=2DP
机器人:顶部=2DP/>
<边角机器人:半径=5DP/>
< /形状>
< /项目>
<项目>
<形机器人:形状=矩形>
<填充
机器人:底部=5DP
机器人:左=5DP
机器人:右=5DP
机器人:顶部=5DP/>
[固体机器人:颜色=@色/主动/>
< /形状>
< /项目>
< /层列表>
round_right_corner
< XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目>
<形机器人:形状=矩形>
[固体机器人:颜色=@色/主动/>
<填充
机器人:底部=2DP
机器人:右=2DP
机器人:顶部=2DP/>
<边角机器人:半径=5DP/>
< /形状>
< /项目>
<项目>
<形机器人:形状=矩形>
<填充
机器人:底部=5DP
机器人:左=5DP
机器人:右=5DP
机器人:顶部=5DP/>
[固体机器人:颜色=@色/基地/>
< /形状>
< /项目>
< /层列表>
有关左键:
<角落
机器人:半径=5dip
机器人:topRightRadius =0dip
机器人:bottomRightRadius =0dip/>
和正确的:
<角落
机器人:半径=5dip
机器人:topLeftRadius =0dip
机器人:bottomLeftRadius =0dip/>
这应该做的伎俩。
不过,安卓bottomRightRadius
和安卓bottomLeftRadius
在API超过12(安卓3.1蜂巢下一个bug )。您应设置在 dimens.xml
半径,并创建一个新的文件夹值-V12
。看到这个解决方案:<一href="http://stackoverflow.com/questions/12505649/somethings-wrong-in-corner-radius-android/12505934#12505934">Something's错误的圆角半径。
然后,你应该做的:
绘制按钮离开
...
&LT;角落
机器人:半径=5dip
机器人:topRightRadius =0dip
机器人:bottomRightRadius =@扪/ right_bottom_leftbutton
机器人:bottomLeftRadius =@扪/ left_bottom_leftbutton/&GT;
绘制按钮右侧
...
&LT;角落
机器人:半径=5dip
机器人:topLeftRadius =0dip
机器人:bottomRightRadius =@扪/ right_bottom_rightbutton
机器人:bottomLeftRadius =@扪/ left_bottom_rightbutton/&GT;
值/ dimens.xml
&LT;! - 左键 - &GT;
&LT;扪名=right_bottom_leftbutton&GT; 5dip&LT; /扪&GT;
&LT;扪名=left_bottom_leftbutton&GT; 0dip&LT; /扪&GT;
&LT;! - 右键 - &GT;
&LT;扪名=right_bottom_rightbutton&GT; 5dip&LT; /扪&GT;
&LT;扪名=left_bottom_rightbutton&GT; 0dip&LT; /扪&GT;
值-V12 / dimens.xml
&LT;! - 左键 - &GT;
&LT;扪名=right_bottom_leftbutton&GT; 0dip&LT; /扪&GT;
&LT;扪名=left_bottom_leftbutton&GT; 5dip&LT; /扪&GT;
&LT;! - 右键 - &GT;
&LT;扪名=right_bottom_rightbutton&GT; 5dip&LT; /扪&GT;
&LT;扪名=left_bottom_rightbutton&GT; 0dip&LT; /扪&GT;
So I have two button styled pretty close to what i want but i need a little help getting it all the way. What I currently have is the first screen show, what I want is the second. Anyone have any ideas? I've attached the relevant code.
layout xml:
....
<Button
android:id="@+id/sched_button"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sync"
android:layout_alignParentTop="true"
android:background="@drawable/round_left_corner_active"
android:text="@string/headersched"
android:layout_toLeftOf="@+id/placeholder"
android:paddingRight="@dimen/scheduleheadermargin"
android:paddingLeft="@dimen/scheduleheadermargin" />
<Button
android:id="@+id/sched_fav_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/sched_button"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/placeholder"
android:background="@drawable/round_right_corner"
android:paddingLeft="@dimen/scheduleheadermargin"
android:paddingRight="@dimen/scheduleheadermargin"
android:text="@string/headerfav" />
...
round_left_corner_active:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/active" />
<padding
android:bottom="2dp"
android:left="2dp"
android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<solid android:color="@color/active" />
</shape>
</item>
</layer-list>
round_right_corner
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="@color/active" />
<padding
android:bottom="2dp"
android:right="2dp"
android:top="2dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<padding
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp" />
<solid android:color="@color/base" />
</shape>
</item>
</layer-list>
For the left button:
<corners
android:radius="5dip"
android:topRightRadius="0dip"
android:bottomRightRadius="0dip" />
And the right one:
<corners
android:radius="5dip"
android:topLeftRadius="0dip"
android:bottomLeftRadius="0dip" />
This should do the trick.
However, android:bottomRightRadius
and android:bottomLeftRadius
have a bug in API lower than 12 (Android 3.1 Honeycomb). You should set the radius inside dimens.xml
and create a new folder values-v12
. See this solution: Something's wrong in Corner radius.
Then, you should do:
drawable button left
...
<corners
android:radius="5dip"
android:topRightRadius="0dip"
android:bottomRightRadius="@dimen/right_bottom_leftbutton"
android:bottomLeftRadius="@dimen/left_bottom_leftbutton" />
drawable button right
...
<corners
android:radius="5dip"
android:topLeftRadius="0dip"
android:bottomRightRadius="@dimen/right_bottom_rightbutton"
android:bottomLeftRadius="@dimen/left_bottom_rightbutton" />
values/dimens.xml
<!-- left button -->
<dimen name="right_bottom_leftbutton">5dip</dimen>
<dimen name="left_bottom_leftbutton">0dip</dimen>
<!-- right button -->
<dimen name="right_bottom_rightbutton">5dip</dimen>
<dimen name="left_bottom_rightbutton">0dip</dimen>
values-v12/dimens.xml
<!-- left button -->
<dimen name="right_bottom_leftbutton">0dip</dimen>
<dimen name="left_bottom_leftbutton">5dip</dimen>
<!-- right button -->
<dimen name="right_bottom_rightbutton">5dip</dimen>
<dimen name="left_bottom_rightbutton">0dip</dimen>
这篇关于Android的按钮样式背景,XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!