![](/img/trans.png)
[英]Get Attributes of the Root element in XML using xpath query expression in Java
[英]Access custom XML attributes from root element
我擴展了ViewGroup並在Android中創建了一個自定義Layout類。 我還定義了一些自定義XML屬性,以幫助定位子視圖。 我需要在根XML元素(自定義Layout)上訪問幾個自定義xml屬性(custom:view_height和custom:view_width),但是當我嘗試從Layout java類訪問它們時,TypedArray為null,就像這樣看不到他們。 他們在孩子的觀點上工作良好。 關於如何從根目錄訪問自定義XML屬性的任何想法? 謝謝!
看到代碼:
<?xml version="1.0" encoding="utf-8"?>
<package.of.my.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent" android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/homecell_selector"
custom:view_width="400" // I need this!!
custom:view_height="200" // I need this!!
>
<Button android:id="@+id/postText" android:layout_marginLeft="20dp"
android:layout_marginTop="45dp" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:alpha="1"
android:background="@color/HEX_FFFFFFFF" android:textColor="@color/HEX_000"
android:textSize="15sp" android:visibility="visible" android:gravity="left"
custom:view_width="0.87610022270753"
custom:eHeight="true"
custom:view_height="0.44296926842493"
custom:eWidth="true"
custom:originX="RIGHT"
custom:x="0.061399777292469"
custom:eX="true"
custom:originY="BOTTOM"
custom:y="0.1682145166653"
custom:eY="true"/>
<Button android:id="@+id/firstNameLastNameShortText" android:layout_marginLeft="21dp"
android:layout_marginTop="8dp" android:layout_height="18dp"
android:layout_width="276dp" android:alpha="1"
android:background="@color/HEX_2087fc" android:textColor="@color/HEX_000"
android:textSize="15sp" android:visibility="visible" android:gravity="left"
custom:view_width="0.86433765030597"
custom:eHeight="true"
custom:view_height="0.15716154473749"
custom:eWidth="true"
custom:originX="LEFT"
custom:x="0.065625" custom:eX="true"
custom:originY="TOP"
custom:y="0.069849575438883"
custom:eY="true"/>
</package.of.my.CustomLayout>
實際上,您不需要這些。 因為您需要寬度和高度。 無論您想添加到自定義視圖中的寬度和高度如何,都應將其直接添加到android:layout_width和android:layout_height標簽中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.