如何在自定义视图中获取"android:"标记值

Gra*_*eme 42 android android-custom-view android-layout

似乎有很多"类似"的问题和答案分散在其中,所有问题都涉及如何从中获取自定义属性AttributeSet.到目前为止我无法找到的是如何获取android:命名空间标记:

    <com.custom.view.StatusThumbnail
        android:id="@+id/statusThumbnailContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)

我想layout_height从这个自定义组件中拉回属性.不幸的是,从我所读到的最近,我必须要做的是:

public StatusThumbnail(Context context, AttributeSet attrs) {
    super(context, attrs);

    String height = attrs.getAttributeValue("android", "layout_height");
Run Code Online (Sandbox Code Playgroud)

但这会回来null.

当然这不是一件罕见的事情吗?

jqp*_*liq 60

命名空间应该是" http://schemas.android.com/apk/res/android"android是在xml文件中声明的别名

  • 棒极了!最后这个工作:`attrs.getAttributeIntValue("http://schemas.android.com/apk/res/android","maxLength",100) (7认同)