繁体   English   中英

自定义线性布局未显示

[英]Custom linear layout not showing

我正在制作自定义布局,但没有显示,我也不知道为什么。

这是定义类的XML文件

<com.example.name.gw2applicaton.SpecializationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="65dp"
            android:layout_height="match_parent">

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="yop2" />

            <Button
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:text="yop2" />

    </LinearLayout>
</com.example.name.gw2applicaton.SpecializationView>

这是类,只是一个构造函数

 public class SpecializationView extends LinearLayout {

    public SpecializationView(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_specialization, this, true);
    }
}

最后是使用类的地方

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

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="horizontal">

     <com.example.name.gw2applicaton.SpecializationView
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical">
      </com.example.name.gw2applicaton.SpecializationView>

        </LinearLayout>
</LinearLayout>

SpecializationView不可见,我不知道为什么。 我在这里做错了什么?

您尝试这样做的方式不适用于自定义视图。 请改用以下约定:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <!-- just include the layout you defined else where -->
        <include layout="@layout/layout_specialization"/>

    </LinearLayout>

其中layout_specialization.xml是:

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

        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="yop2" />

        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="yop2" />

</LinearLayout>

注意:当您需要修改现有视图或视图组以具有特殊的编程功能(例如定位,动态内容,小众窗口部件等)时,应使用自定义视图定义。当您要使用自己喜欢的视图时只是使用现有的小部件功能,请按照我的描述进行操作。 include xml标记非常适合定义xml布局并在您的项目中重复使用,因此可以最大程度地减少代码重复。

编辑:

布局未显示的原因是您仅定义了用于以编程方式创建视图的构造函数(通过Java代码,而不是xml)。 为了允许您的自定义视图的xml定义扩展类,如下所示,并需要其他构造函数:

public class SpecializationView extends LinearLayout {

    /* Programmatic Constructor */
    public SpecializationView(Context context) {
        super(context);
        init(context, null, 0);
    }

    /* An XML Constructor */
    public SpecializationView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    /* An XML Constructor */
    public SpecializationView(Context context, AttributeSet attrs, int resId) {
        super(context, attrs, resId);
        init(context, attrs, resId);
    }

    /** 
    * All initialization happens here!
    */
    private void init(Context context, AttributeSet attrs, int resId){
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_specialization, this, true);
    }
}

现在,此定义包括创建自定义视图的xml功能(现在应该对您有用)。 它将起作用的原因是现在您将属性集或通过xml定义的属性发送给构造函数。 由于未包含它,因此在xml中定义时,它不知道如何对自定义视图执行操作,并且您无法访问您可以定义为自定义的布局属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM