android - 如果我在布局 xml 文件中用自定义 View 类标记替换 "include"标记, View 不会显示,为什么?

标签 android xml layout android-custom-view

我已按照 this guide 中的“创建复合 View ”部分进行操作.在我的应用程序的一项 Activity 中,有如下定义的 View :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- some other subviews... -->

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <!-- some other subviews... -->

        <include
            android:id="@+id/myHeaderView"
            layout="@layout/view_my_header"></include>

    </FrameLayout>
</LinearLayout>

虽然自定义 myHeaderView 的布局在 xml 文件中定义如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="250dip"
    android:orientation="vertical"
    android:background="@android:color/transparent">

    <View
        android:layout_height="5dip"
        android:layout_width="match_parent"
        app:layout_aspectRatio="100%"
        android:background="@android:color/holo_green_light" />

    <View
        android:id="@+id/yellowView"
        app:layout_heightPercent="50%"
        app:layout_aspectRatio="100%"
        android:layout_width="120dp"
        android:layout_height="120dp"
        android:background="@android:color/holo_orange_light"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="12dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true" />

    <View
        android:id="@+id/redView"
        app:layout_heightPercent="40%"
        app:layout_aspectRatio="200%"
        android:background="@android:color/holo_red_light"
        android:layout_width="200dip"
        android:layout_height="100dip"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true" />

    <View
        android:layout_height="5dip"
        android:layout_width="match_parent"
        app:layout_aspectRatio="100%"
        android:background="@android:color/holo_green_light"
        android:layout_alignParentBottom="true"/>

</android.support.percent.PercentRelativeLayout>

基本上它应该是这样的:

enter image description here

它运行良好,自定义 View 按预期显示。但是,如果我将 xml 中自定义 View 元素的 include 标记替换为以下标记:

<com.myDomain.myHeaderView
    android:id="@+id/myHeaderView"
    android:layout_width="match_parent"
    android:layout_height="250dip"
    android:orientation="vertical"
    android:background="@android:color/transparent"/>

并更新自定义类 MyHeaderView 的构造函数,如下所示:

public MyHeaderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initView(context);
}

// some other constructor implementations, which they all call "initView(context)"

private void initView(Context context) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.view_my_header, this, true);
}

自定义标题 View 的 subview (例如黄色 View )将消失。我检查了 Device Monitor 的布局 View ,标题 View 及其 subview 的大小似乎是正常的。但由于某些我不知道的原因,它们是不可见的。

我做错了什么吗?我知道 include 标签表示我正在重新使用相应文件定义的布局组件,但我希望将自定义 View 引用为类 MyHeaderView而不是 ViewGroup,因此我可以调用其中定义的方法从而更新 View ,我该如何实现?

谢谢!

最佳答案

你可以试试把<include>里面<MyHeaderView>而不是在 MyHeaderView 中膨胀:

<com.myDomain.MyHeaderView
android:id="@+id/myHeaderView"
android:layout_width="match_parent"
android:layout_height="250dip"
android:orientation="vertical"
android:background="@android:color/transparent">

    <include        
        layout="@layout/view_my_header"/>

</com.myDomain.MyHeaderView>

关于android - 如果我在布局 xml 文件中用自定义 View 类标记替换 "include"标记, View 不会显示,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40478096/

相关文章:

带有 RemoteViews 的 Android 5+ 自定义通知 XML 布局,为 ImageButton 设置正确的图标色调

ruby-on-rails - Rabl.渲染 : how to use view helper methods?

Java安装错误: INSTALL_FAILED_MISSING_SHARED_LIBRARY maps

android - 如何从我的应用程序内部通过电子邮件发送 Android 设备的日志文件?

java - 解码为抽象元素列表

java - java中框布局行中的面板居中

html - div内容重叠

java - 树不占据整个父组合

android - OsmDroid setCenter 以错误的位置为中心

java - 如何从 firebase 实时数据库检索用户名