【问题标题】:How to get my own defined attribute value in my style如何以我的风格获取我自己定义的属性值
【发布时间】:2011-11-25 15:41:50
【问题描述】:

我想使用自定义(自己的)属性为对话框创建 3 个不同的主题。 我想通过将其添加到主题样式来设置标题颜色: <item name="titleColor">#FF0000</item>

我的主题.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme" parent="@android:style/Theme">
    <item name="android:alertDialogStyle">@style/dialog</item>
</style>
<style name="MyRedTheme" parent="MyTheme">
    <item name="titleColor">#FF0000</item>
</style>
<style name="MyGreenTheme" parent="MyTheme">
    <item name="titleColor">#00FF00</item>
</style>
<style name="MyBlueTheme" parent="MyTheme">
    <item name="titleColor">#0000FF</item>
</style>

我在 attrs.xml 中定义了 titleColor 属性:

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <declare-styleable name="MyCustomAttributes">
  <attr name="titleColor" format="color|reference" />
 </declare-styleable>
</resources>

我为对话框应用了其中一个主题。 如何将我的 titleColor 属性值传递给“android:color”属性?

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:custom="http://schemas.android.com/apk/res/com.dicare"
   android:shape="rectangle">
       <solid android:color="I want to pass titleColor value here"/>
</shape>

【问题讨论】:

  • 参考这个链接,你可以得到更多关于使用自定义xml属性的想法。HERE
  • 我想插入由我的自定义属性定义的值,而不是用新属性扩展视图组件
  • 我也有同样的问题。还没找到解决办法

标签: android coding-style resources themes


【解决方案1】:

?titleColor see here

您可以在 colors.xml 文件中定义颜色并像普通资源一样引用它们:@color/MyRed

您可以为自己的视图创建一个自定义属性,您希望从布局 xml 中对其进行自定义。例如,您可以扩展 TextView 以使用一种颜色 (titleColor) 编写第一行文本,而不是其余文本 (android:textColor)。

<color name="MyRed">#FF0000</color>

<style name="MyRedTheme" parent="MyTheme">
    <item name="titleColor">@color/MyRed</item>
</style>

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:custom="http://schemas.android.com/apk/res/com.dicare"
   android:shape="rectangle">
       <solid android:color="@color/MyRed"/>
</shape>

【讨论】:

  • 好吧“?”应该可以解决问题,但它给了我一个:java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
  • 我也有同样的问题。仍然没有找到任何解决方案。
  • 为什么这个解决方案被接受。它是否回答了如何访问样式中定义的自己的颜色的问题?
【解决方案2】:

因此,您要做的第一件事就是编辑 attrs.xml 文件。在这里,您将添加要通过 xml 定义的所有属性。在这里,我们添加了一个标题,以及带有文本和可绘制对象的左右按钮。

<declare-styleable name="activity_header">
    <attr name="title" format="string" />               

    <attr name="left_button_text" format="string" />
    <attr name="left_button_drawable" format="reference" />

    <attr name="right_button_text" format="string" />
    <attr name="right_button_drawable" format="reference" />

    <attr name ="hide_buttons">
        <enum name="yes" value="1" />
        <enum name="no" value="0" />
    </attr>                        
</declare-styleable> 

接下来,您将要创建布局。这里重要的是添加一个引用您的应用程序的命名空间。在这里,我将其命名为 app。您只需要在 http://schemas.android.com/apk/res/ 之后包含您的包名称。现在您可以在 xml 文件中使用上面定义的任何属性。

LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app = "http://schemas.android.com/apk/res/com.biggu.shopsavvy.ui4"  
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical" 
      >


<com.biggu.shopsavvy.ui4.ActivityHeader 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"                   
    android:id="@+id/header"
    app:title = "History"                                  

    app:left_button_text="Share"
    app:left_button_drawable="@drawable/ic_menu_share" 

    app:right_button_drawable="@drawable/small_btn_archive"
    app:right_button_text="Organize"   />

现在我们已经在 xml 文件中定义了属性,我们需要从我们制作的自定义组件中检索它们。您应该只需要使用您创建的资源获取获得的样式属性,这里我们使用了 activity_header。

  public class ActivityHeader extends LinearLayout {

    TextView mTitleEditText;
    Button mLeftButton;
    Button mRightButton;

    View mDelimeter;
    private ViewGroup mAdditionalPanel;



    public ActivityHeader(Context context, AttributeSet attrs) {
        super(context, attrs);      
        ViewGroup.inflate(context, R.layout.header , this);             

        findViews();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.activity_header);      

        if ( a.getInt(R.styleable.activity_header_hide_buttons, 0) == 1) //hide buttons
        {
            mLeftButton.setVisibility(GONE);
            mRightButton.setVisibility(GONE);
        }
        else
        {
            setLeftButtonDrawable(a.getResourceId(R.styleable.activity_header_left_button_drawable, android.R.drawable.ic_menu_info_details));      
            setLeftButtonText(a.getString(R.styleable.activity_header_left_button_text));                       

            setRightButtonDrawable(a.getResourceId(R.styleable.activity_header_right_button_drawable, android.R.drawable.ic_menu_info_details));                    
            setRightButtonText(a.getString(R.styleable.activity_header_right_button_text)); 
        }


        setTitle(a.getString(R.styleable.activity_header_title));                                                                                         
        a.recycle();        
    }
}

就是这样。编码愉快。

【讨论】:

  • 这不回答问题,重点是如何在不同的地方使用主题属性,而不是如何在自定义视图中使用自定义属性:)
【解决方案3】:

我认为您需要做的就是将 android:color 更改为 custom:color:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:custom="http://schemas.android.com/apk/res/com.dicare"
   android:shape="rectangle">
       <solid custom:color="I want to pass titleColor value here"/>
</shape>

【讨论】:

  • 我想插入由我的自定义属性定义的值,而不是用新属性扩展视图组件
猜你喜欢
  • 2021-11-21
  • 2013-04-16
  • 1970-01-01
  • 2013-02-19
  • 2013-05-30
  • 2020-11-23
  • 2014-06-26
  • 1970-01-01
  • 2011-07-03
相关资源
最近更新 更多