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

标签 android user-interface subclass android-framelayout lib

我正在扩展一个“MapView”,它本身扩展了FrameLayout。但我无法访问我添加的 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;

但是在尝试获取LinearLayout线性为空

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(...),因为这是一个 View 而不是一个 Activity ?

最佳答案

MapView 的官方文档 here明确表示

Note: You are advised not to add children to this view.

这意味着仅部分支持MapView自定义内部 View ,或者根本不支持。我不建议您使用不受支持的东西,因为它可能会像您所拥有的那样产生奇怪的错误。特别是在 MapView 中,它具有复杂的触摸监听器和广泛的内部 UI - 您可以轻松地破坏某些内容(例如使用 ScrollView 进行缩放或相机滑动)

坦白说,我不确定你为什么要这样做 - 你可以轻松地将所需的所有 View 放置在普通的 FrameLayout 中,一切都会正常工作(除了 ScrollView滑动手势处理器可能会干扰 MapView 手势)

所以这样做会更明智

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.company.com.library.mapbox.MyMapView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/mapView"/>

        <FrameLayout
            android:id="@+id/wrapper_layout"
            android:layout_width="50dp"
            android:layout_height="250dp"
            android:layout_gravity="right"
            android:layout_marginTop="100dp">

            <ScrollView
                android:id="@+id/level_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/level_layout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>

            </ScrollView>

        </FrameLayout>

</FrameLayout>

这样,您将能够分别利用 MapViewFrameLayout 的所有可能性。不要忘记处理 fragment/Activity 中 MapView 的生命周期更改,正如它所告诉的 here .

您可能还会注意到,我用 FrameLayout 包装了 ScrollView 并赋予了该包装器布局的宽度和高度,而 ScrollView 具有 match_parent 。我这样做是因为具有定义的高度和宽度的 ScrollView 可能会很麻烦,并且很容易出现错误,因此建议将其包装在其他布局中以达到此类目的。

虽然这不是您问题的确切解决方案,但我希望这个答案能对您有所帮助。

关于android - 如何访问库项目中自定义 FrameLayout 的子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58098308/

相关文章:

xamarin.ios - Interface Builder 看不到我的子类 UILabel

python - 使用 vars 的 TensorFlow 模型子类化 API 不显示参数或层

java - 通过 HttpClient 接受所有 Cookie

java - 编解码器参数不正确,无法写入输出文件 : FFMPEG - Android 的 header

ios - 我可以知道这个最好的 UI/UX 方法是什么吗?

C# 多屏幕 View 单一窗体

c++ - 将子类传递给采用其父类(super class)的函数

javascript - Android(三星Galaxy)上的JQuery Mask输入插件,返回的keyCode始终为229

Android Studio 启动失败

java - 3D Java 编程教程不起作用