我需要创建一个像这个一样的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度等方向上,按钮被矩形画布截断了。即使我旋转它,我也只能在矩形画布范围内进行。