【问题标题】:How to access android resource attributes如何访问android资源属性
【发布时间】:2017-04-22 08:41:51
【问题描述】:

我要访问

getContext().obtainStyledAttributes(attrs,android.R.styleable.TextView)

但我在styleable 中有错误。 android.R 中没有可用的 styleable

我将在我的复合视图中使用这个值。

public class CustomTextView extends TextView {
 public CustomTextView(Context context) {
    super(context);
    init(null);
 }

 public CustomTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(attrs);
 }

 public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
 }

 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init(attrs);
 }

 private void init(AttributeSet attrs) {
    if (attrs != null && !isInEditMode()) {
        TypedValue typedValue = new TypedValue();
        int[] textSizeAttr = new int[] { android.R.attr.textSize };
        int indexOfAttrTextSize = 0;
        TypedArray a = getContext().obtainStyledAttributes(typedValue.data, textSizeAttr);
        int textSize = a.getDimensionPixelSize(indexOfAttrTextSize, -1);
        a.recycle();
    }
 }

}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="hgyanani.customcompoundview.MainActivity">

    <hgyanani.customcompoundview.CustomTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="16sp" />
</RelativeLayout>

【问题讨论】:

  • 尝试清理项目并重建它

标签: android android-attributes typedarray


【解决方案1】:

你可以这样试试:

private void init(AttributeSet attrs) {

    if (attrs != null && !isInEditMode()) {
        int[] attrsArray = new int[] {
                android.R.attr.textSize, // 0
        };
       TypedArray a = getContext().obtainStyledAttributes(attrs, attrsArray);
       int textSize = a.getDimensionPixelSize(0 /*index of attribute in attrsArray*/, View.NO_ID);
  }
}

textSize 将返回以 pixel 为单位的值。

如果您想要 sp 中 textsize 的确切值,那么在自定义视图的构造函数(接受 AttributeSet 的视图)中,您可以从 Android 的命名空间中检索属性,如下所示:

String xmlProvidedSize = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");

xmlProvidedSize 的值将类似于“16.0sp”,也许只需稍加编辑字符串,您就可以提取数字。

【讨论】:

  • 我仍然不能使用int textSize=a.getDimensionPixelSize(android.R.styleable.TextView_textSize); 我的主要动机是从中获取'textSize'属性值。
  • @HarishGyanani :我认为它会解决您的问题。 :-)
  • 我发布了更多代码,请检查。我试过调试,但在 textSize 变量中得到 -1。
  • 你想获取你在 XML 中使用过的 textSize 值吗?或者你想在运行时设置它?
  • 正如你在我的布局文件中看到的,我使用了android:textSize="16sp"
猜你喜欢
  • 2016-05-11
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
相关资源
最近更新 更多