I want to create 3 different themes for a dialog using a custom (own) attribute. I would like to set title colors by adding this to theme's style: <item name="titleColor">#FF0000</item>
我想使用自定义(自己)属性为对话框创建3个不同的主题。我想通过将其添加到主题的样式来设置标题颜色:
my themes.xml:
我的themes.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>
I defined titleColor attribute in attrs.xml:
我在attrs.xml中定义了titleColor属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyCustomAttributes">
<attr name="titleColor" format="color|reference" />
</declare-styleable>
</resources>
I apply one of the themes for the dialog. How can I pass my titleColor attribute's value to an "android:color" attribute?
我为对话框应用了一个主题。如何将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>
3 个解决方案
#1
10
?titleColor see here
?titleColor见这里
or
要么
You'd define your colours in the colors.xml file and reference them like a normal resource: @color/MyRed
您可以在colors.xml文件中定义颜色,并像普通资源一样引用它们:@color / MyRed
You'd make a custom attribute for your own views which you want to be customisable from layout xmls. For example, you might extend TextView to write the first line of text in one colour (titleColor) than the rest of the text (android:textColor).
您可以为自己的视图创建自定义属性,您可以从布局xmls中自定义该属性。例如,您可以将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>
#2
1
So the first thing that you will have to do is edit your attrs.xml file. Here you will add all of the attributes that you want to define via xml. Here we have added a title, as well as right and left buttons with text and a drawable.
所以你要做的第一件事就是编辑你的attrs.xml文件。在这里,您将添加要通过xml定义的所有属性。在这里,我们添加了一个标题,以及带有文本和drawable的左右按钮。
<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>
Next you'll want to create your layout. The important thing here is to add a namespace that refers to your app. Here I have named it app. You just need to include your package name after http://schemas.android.com/apk/res/ . Now you can use any of the attributes you defined above in your xml file.
接下来,您将要创建布局。这里重要的是添加一个引用您的应用程序的命名空间。在这里,我将它命名为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" />
Now that we have our attributes defined in our xml file we need to retrieve them from our custom component that we have made. You should simply have to get the obtained style attributes using your resource that you created, here we used activity_header.
现在我们已经在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();
}
}
That's it. Happy coding.
而已。快乐的编码。
#3
0
I think all you need to do is change the android:color for custom:color:
我认为您需要做的就是更改自定义的android:color: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>
#1
10
?titleColor see here
?titleColor见这里
or
要么
You'd define your colours in the colors.xml file and reference them like a normal resource: @color/MyRed
您可以在colors.xml文件中定义颜色,并像普通资源一样引用它们:@color / MyRed
You'd make a custom attribute for your own views which you want to be customisable from layout xmls. For example, you might extend TextView to write the first line of text in one colour (titleColor) than the rest of the text (android:textColor).
您可以为自己的视图创建自定义属性,您可以从布局xmls中自定义该属性。例如,您可以将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>
#2
1
So the first thing that you will have to do is edit your attrs.xml file. Here you will add all of the attributes that you want to define via xml. Here we have added a title, as well as right and left buttons with text and a drawable.
所以你要做的第一件事就是编辑你的attrs.xml文件。在这里,您将添加要通过xml定义的所有属性。在这里,我们添加了一个标题,以及带有文本和drawable的左右按钮。
<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>
Next you'll want to create your layout. The important thing here is to add a namespace that refers to your app. Here I have named it app. You just need to include your package name after http://schemas.android.com/apk/res/ . Now you can use any of the attributes you defined above in your xml file.
接下来,您将要创建布局。这里重要的是添加一个引用您的应用程序的命名空间。在这里,我将它命名为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" />
Now that we have our attributes defined in our xml file we need to retrieve them from our custom component that we have made. You should simply have to get the obtained style attributes using your resource that you created, here we used activity_header.
现在我们已经在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();
}
}
That's it. Happy coding.
而已。快乐的编码。
#3
0
I think all you need to do is change the android:color for custom:color:
我认为您需要做的就是更改自定义的android:color: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>