如何访问库项目中自定义FrameLayout的子元素?

问题描述 投票:0回答:1

我正在扩展本身扩展FrameLayout的“ MapView”。但是我无法访问添加的ui元素:level_layout和* level_scroll。我可以很轻松地将代码直接添加到我的MainActivity中,但不能在库中使用它。

<?xml version="1.0" encoding="utf-8"?>
<android.company.com.library.mapbox.MyMapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mapView">

        <ScrollView
            android:id="@+id/level_scroll"
            android:layout_width="50dp"
            android:layout_height="250dp"
            android:layout_gravity="right"
            android:layout_marginTop="100dp"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/level_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"></LinearLayout>
        </ScrollView>
</android.company.com.library.mapbox.MyMapView>

MyMapView.java中,我从以下位置获取值:

int level = R.id.level_layout;

但是linear尝试获取LinearLayout]时为null

LinearLayout linear = (LinearLayout) findViewById(R.id.level_layout);

我尝试添加

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
}

((在调用此方法后,我尝试访问我的ui元素)

在不同的地方调用以下内容(例如,构造函数)

LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService
        (Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.nameOfTheXmlFile, null);

我没有多个具有相同名称的布局文件。

我不能使用setContentView(...)

,因为这是一个视图而不是一个活动?

我正在扩展本身扩展FrameLayout的“ MapView”。但是我无法访问添加的ui元素:level_layout和* level_scroll。我可以轻松地将代码直接添加到我的MainActivity中,...

android user-interface subclass android-framelayout lib
1个回答
0
投票

MapView here的官方文档明确指出

© www.soinside.com 2019 - 2024. All rights reserved.