我有一个包含两个TextView的自定义组件,它们具有自定义大小设置方法(两个文本视图的比例约为1:2).由于这是RelativeLayout的子类,它没有textSize属性,但我想知道是否仍然可以android:textSize在该组件的XML实例化中设置该属性,然后textSize从AttributeSet中获取属性以使用setSize()在构造函数中使用我的自定义方法.
我已经看到了使用自定义属性执行此操作的技术,但是如果我想获取已经在android词典中的属性怎么办?
我希望从样式资源中提取多个属性(仅对属于TextAppearance组的属性感兴趣)
风格定义如此
<style name="Label" parent="@android:style/TextAppearance.Small">
<item name="android:textColor">@color/floatlabel_text</item>
<item name="android:textSize">8dp</item>
<item name="android:textStyle">bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)
首先我尝试了TextView(第663-731行)如何实现它,但后来我发现我们无法访问com.android.internal.R
这就是我切换到这个解决方案的原因:https://stackoverflow.com/a/7913610/3922891
所以我创建了textAppearanceAttr来替换com.android.internal.R.styleable.TextAppearance(只包含我感兴趣的10/13 TextAppearance属性)
int[] textAppearanceAttr = new int[]{
android.R.attr.textColor,
android.R.attr.textSize,
android.R.attr.typeface,
android.R.attr.fontFamily,
android.R.attr.textStyle,
android.R.attr.textAllCaps,
android.R.attr.shadowColor,
android.R.attr.shadowDx,
android.R.attr.shadowDy,
android.R.attr.shadowRadius};
Run Code Online (Sandbox Code Playgroud)
这是我如何使用它.我得到了样式的资源ID(资源由clTextAppearance属性引用)
int ap = a.getResourceId(R.styleable.CustomLabelLayout_clTextAppearance, android.R.style.TextAppearance_Small);
TypedArray appearance = mContext.obtainStyledAttributes(ap, textAppearanceAttr);
Run Code Online (Sandbox Code Playgroud)
以下是我获取属性的方法(仍然按照上面的链接回答):
mLabelTextColor = appearance.getColorStateList(0);
mLabelTextSize = appearance.getDimensionPixelSize(1, 15);
mLabelTypeface = appearance.getInt(2, -1);
mLabelFontFamily = appearance.getString(3);
mLabelTextStyle = appearance.getInt(4, -1);
(5 more...)
Run Code Online (Sandbox Code Playgroud)
似乎只有第一个属性被设置,其他每个属性都设置为默认值或null.
个别数组:
int[] textSizeAttr = new int[] …Run Code Online (Sandbox Code Playgroud) 如何以编程方式获取Theme属性的值?
例如:
主题:
<style name="Theme">
... truncated ....
<item name="textAppearanceLarge">@android:style/TextAppearance.Large</item>
</style>
Run Code Online (Sandbox Code Playgroud)
码:
int textSize = ???? // how do I get Theme.textAppearanceLarge?
Run Code Online (Sandbox Code Playgroud)
编辑:太多的答案没有解决这个问题.
我正在构建我的应用程序,以便它可以受益于多个主题.
我已经到了我想添加自定义项目的阶段,例如
<style name="My.Theme.Default">
...
<item name="borderColorDialog">@color/red</item>
...
</style>
<style name="My.Theme.Blue">
...
<item name="borderColorDialog">@color/blue</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
所以我已经将属性添加borderColorDialog到我的attr文件中,如下所示:
<attr name="borderColorDialog" format="color" />
Run Code Online (Sandbox Code Playgroud)
现在我想测试它.我有一个drawable如下:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="rectangle" >
<solid
android:color="?borderColorDialog" />
</shape>
</item>
<item
android:bottom="2dp">
<shape
android:shape="rectangle" >
<solid
android:color="@color/black" />
</shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)
这对我来说在逻辑上看起来都很合理,但?borderColorDialog引用会使模拟器崩溃:
E/AndroidRuntime(1461): FATAL EXCEPTION: main
E/AndroidRuntime(1461): java.lang.RuntimeException: Unable to start activity ComponentInfo{.MainActivity}: android.view.InflateException: Binary XML file line #26: Error inflating class com.android.internal.widget.ActionBarContainer
E/AndroidRuntime(1461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
E/AndroidRuntime(1461): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) …Run Code Online (Sandbox Code Playgroud) 无论如何要在布局xml文件中设置“android.support.v7.widget.Toolbar”一半的预定义属性的高度,例如“?android:attr/actionBarSize”?事实上,我希望我的工具栏的高度是这样的:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height=("?android:attr/actionBarSize")/2
>
Run Code Online (Sandbox Code Playgroud) android android-layout android-view android-resources android-styles
我正在尝试创建一个继承自RelativeLayout的自定义组件.
在我的xml布局文件中,我有:
<Mycomponent
android:src="@drawable/my_test_image">
<TestView>
</Mycomponent>
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在Mycomponent的构造函数中创建一个Drawable类?
我试图阅读ImageView的源代码,但似乎尝试了一些android Internal.R.
无论如何我在我的代码中可以做到这一点.
谢谢.
我需要知道当前默认Android字体的大小(以像素为单位).我在这里复制了一个非常相似的问题中列出的方法.但是,我总是得到-1,而不是以像素为单位获得大小.我有什么想法我做错了吗?
这是代码:
Context context = getActivity();
TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textAppearance, typedValue, true);
int[] textSizeAttr = new int[] { android.R.attr.textSize };
TypedArray a = context.obtainStyledAttributes((AttributeSet) typedValue.string, textSizeAttr);
textSize = a.getDimensionPixelSize(0, -1);
Log.e("Metrics", "text size in dp = " + String.valueOf(textSize));
a.recycle()
Run Code Online (Sandbox Code Playgroud)