簡體   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