【发布时间】:2014-08-15 19:44:52
【问题描述】:
我必须像这样创建ToggleButton(应该在任何设备上调整大小)。
然后在ViewGroup的onLayout方法中进行布局。
您能告诉我如何创建它吗?
我尝试过这样做:
buttons.xml
<ToggleButton
android:id="@id/add_favorites_button_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@drawable/menu_button_selector"
android:drawableTop="@drawable/star"
android:textOff=""
android:textOn=""
android:contentDescription="Add Favorites"/>
menu_button_selector.xml
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0">
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true">
<rotate
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/round_button_active"/>
</item>
<item android:state_checked="false">
<rotate
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:drawable="@drawable/round_button"/>
</item>
</selector>
</item>
.....
</level-list>
round_button_active.xml
我有一些背景,只是旋转它。因为按钮应该位于已知度数:0、45、90、...
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
<solid android:color="@color/tb_red_button"/>
<size android:width="60dp" android:height="60dp"/>
</shape>
</item>
</layer-list>
然后在onLayout 方法中我对其进行布局:
final View button = parentLayout.getChildAt(i);
button.getBackground().setLevel(i);
button.setOnClickListener(this);
button.layout(x1, y1, x2, y2);
我收到了什么:
在 0、90、180 等度数上的按钮看起来很清楚它们应该是什么
但是
在 45、135 等度数上的按钮被矩形画布切割。即使我旋转它,我也会在矩形画布的范围内进行。
【问题讨论】:
-
我将首先创建您自己的自定义视图组,然后创建一个自定义切换按钮
-
是的,非常感谢您的好建议。最好的一个
-
也许不需要自定义切换按钮 ;)
-
是的,你是对的,对不起,我花了很多时间创造它(总是错的),这就是为什么我这样回复你的回答;)
-
祝你好运!因为在 Android 上创建自定义组件并不是最简单的事情
标签: android togglebutton