【问题标题】:How to change the shapes of the corners of textview, button, edit text by applying themes dynamically?如何通过动态应用主题来改变textview、button、edit text的角形状?
【发布时间】:2012-08-31 04:23:07
【问题描述】:

这是我的代码。我从未在我的应用程序中看到任何变化。我在resources/drawable 中创建了这个。我应该如何通过更改文本colorfontsshapes 等为所有组件动态应用主题样式。

 <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="oval" android:padding="20dp">
     <solid android:color="#FFA500"/>
     <corners android:bottomRightRadius="50dp" android:bottomLeftRadius="50dp"
     android:topLeftRadius="50dp" android:topRightRadius="50dp"/>
     <stroke android:color="#FFA500" android:dashWidth="50dp" />
 </shape>

【问题讨论】:

    标签: android android-widget styles themes


    【解决方案1】:

    在drawable中创建一个shape.xml

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" >
         android:shape="rectangle">
     <gradient
        android:startColor="#99CCFF"
        android:endColor="#99CCFF"
        android:angle="45"/>
     <padding android:left="6dp"
        android:top="6dp"
        android:right="6dp"
        android:bottom="6dp" />
     <corners android:radius="30dp" />
     </shape>
    

    现在为您的 Button 代码添加 android:background,如下所示。

      <Button
        android:id="@+id/bt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape"/>  // shape is name of xml file created
    

    【讨论】:

    • schemas.android.com/apk/res/android" android:shape="oval" android:padding="20dp" > //在res/drawable中创建形状
    • //在Main.xml中创建
    【解决方案2】:

    您上面的代码运行良好。可能问题出在您声明背景的方式上:您是在 XML 文件中执行,还是通过 Java 代码动态执行?

    【讨论】:

    • 不是动态的,因为我注意到主题/样式不支持动态。所以我在 Xml 工作。
    • 好的..刚刚看到你的代码...只是尝试动态声明背景.. Button button=(Button)findviewbyid(r.id.yourid); button.setBackgroundResource(R.drawable.yourxmlfile);...我认为代码适合你
    • 是的,我注意到了。它已经改变,特别是按钮形状。但我不应该在 Java (Activity) 中调用它。我需要在 Xml 文件中执行此操作。当我调用主题时,它必须完全动态地应用于我的应用程序中的所有按钮。
    • 您的背景代码在我的 xml 文件中也可以正常工作。我认为您的 @style/theme 存在问题...发布主题代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 2016-12-23
    • 2011-11-02
    • 2013-01-13
    相关资源
    最近更新 更多