相关疑难解决方法(0)

是否可以在xml描述中旋转drawable?

我正在创建一个应用程序,其资源可以重复使用(因为按钮总是相同,但是镜像或旋转).我确实想要使用相同的资源,所以我不必再添加3个与原始资源完全相同但又已旋转的资源.但我也不想将代码与可以在XML中声明的内容混合,或者使用会花费处理时间的矩阵进行转换.

我在XML中声明了一个两个状态按钮.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/and_card_details_button_down_left_onclick" /> <!-- pressed -->
    <item android:drawable="@drawable/and_card_details_button_down_left" /> <!-- default -->
</selector>
Run Code Online (Sandbox Code Playgroud)

我想重复使用drawable,因为它会相同但旋转90º和45º并且我将按钮分配为drawable.

<Button android:id="@+id/Details_Buttons_Top_Left_Button"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/details_menu_large_button" />
Run Code Online (Sandbox Code Playgroud)

我知道我可以用一个RotateDrawable或一个旋转它,Matrix但正如我已经解释过的那样,我不喜欢这种方法.

是否有可能直接在XML上实现这一点,或者您认为最好的方法是什么?放置所有资源但旋转,在代码中旋转它们?

---编辑--- @dmaxi的答案很棒,这是如何将它与项目列表相结合:)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <rotate 
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

    <item>
        <rotate
        android:fromDegrees="90"
        android:toDegrees="90"
        android:pivotX="50%"
        android:pivotY="50%"
        android:drawable="@drawable/and_card_details_button_up_onclick"/>
    </item>

</selector>
Run Code Online (Sandbox Code Playgroud)

android drawable android-layout android-xml

99
推荐指数
4
解决办法
8万
查看次数

标签 统计

android ×1

android-layout ×1

android-xml ×1

drawable ×1