【问题标题】:how to get custom xml values in android?如何在android中获取自定义xml值?
【发布时间】:2010-11-17 05:49:10
【问题描述】:

我想在xml中获取launcher:cellWidth的个数:

<mobi.intuitit.android.p.launcher.CellLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/mobi.intuitit.android.p.launcher"

    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    launcher:cellWidth="80dip"
/>

我知道我可以通过以下方式在自定义视图中获取它:

public CellLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CellLayout, defStyle, 0);

        mCellWidth = a.getDimensionPixelSize(R.styleable.CellLayout_cellWidth, 100);

但是我怎样才能在 Activity 中获得它?不是自定义视图。 谢谢!

【问题讨论】:

    标签: android xml custom-attributes


    【解决方案1】:

    您的自定义视图应该有一个getter CellLayout.getCellWidth() 和从您的Activity,找到后您可以获取它的值。

    CellLayout cellLayout = (CellLayout)findViewById(R.id.cell_layout);
    
    cellLayout.getCellWidth();
    

    【讨论】:

    • 也许我可以使用 xml 解析器,但它更复杂。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-10
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多