【发布时间】:2018-06-23 19:40:52
【问题描述】:
我在从 xml 布局的父元素获取自定义布局属性时遇到了很大的麻烦 好吧,假设我有这样的布局
<com.blackstephen.customlayoutexperimental.StripedLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/stripedLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:divider_width="5dp">
<!-- put first view to left. -->
<TextView
android:background="@mipmap/o"
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_position="left"
app:percentage="40"
android:text="@string/l1"/>
<com.blackstephen.customlayoutexperimental.StripedLayout/>
请注意,我在子 (TextView) 应用程序中有这些样式属性:layout_position="left" 应用程序:百分比=“40” 我可以通过将此内部类放入自定义显示的代码中来获取这些值
/**
* Custom per-child layout information.
*/
public static class LayoutParams extends MarginLayoutParams {
/**
* The gravity to apply with the View to which these layout parameters
* are associated.
*/
public int gravity = Gravity.TOP | Gravity.START;
public int position = 0;
public int percentage = 33;
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
// Pull the layout param values from the layout XML during
// inflation. This is not needed if you don't care about
// changing the layout behavior in XML.
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.StripedLayoutLP);
gravity = a.getInt(R.styleable.StripedLayoutLP_android_layout_gravity, gravity);
position = a.getInt(R.styleable.StripedLayoutLP_layout_position, position);
percentage = a.getInt(R.styleable.StripedLayoutLP_percentage, percentage);
a.recycle();
}
但这只会给我每个孩子的样式属性 我还想要来自父级的样式属性,称为 app:divider_width="5dp"。 我该怎么做?
【问题讨论】: