【问题标题】:How do I get styled attributes from xml layout parent如何从 xml 布局父级获取样式属性
【发布时间】: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"。 我该怎么做?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    您可以在您的可绘制文件夹中创建一个 XML 文件,并将您想要的所有属性放在该 XML 中。如果您想要一个分隔线,请在 drawable 中创建一个 divider.xml 并定义您想要的所有属性。然后用户 android:divider"@drawable/divider" 在 TextView 上调用这些属性。

    示例:

    <shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@android:color/transparent" /> // color of the divider
    

    在TextView中使用

        android:divider="@drawable/divider"
        android:dividerHeight="10dp"
    

    【讨论】:

    • 但我想创建一个库项目以使我的布局更具可重用性,因此我需要将所有内容抽象出来
    • 要使其成为您图书馆的一部分,您需要创建一个主题。我的解决方案对您的问题有效吗?如果是,请将其标记为正确,以供其他人参考
    【解决方案2】:

    对于每个孩子,我使用的布局参数 一个名为 LayoutParams 的内部类,它扩展了 MarginLayoutParams
    但对于父布局参数,我在构造函数中使用此代码

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.StripedLayout);
        dividerWidth = a.getDimensionPixelSize(R.styleable.StripedLayout_divider_width, dividerWidth);
        mDividerWidth = dividerWidth;
        a.recycle();
    

    【讨论】:

      猜你喜欢
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多