簡體   English   中英

創建自定義Android視圖/小部件

[英]Create custom Android view/widget

我試圖創建一個自定義窗口小部件,可以在活動布局中使用它。 我為擴展了View的小部件創建了一個類。

import android.app.Service;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import be.robinj.ubuntu.R;

public class AppLauncher extends View
{
    private String name;
    private String description;
    private boolean special;
    private AppIcon icon;
    private View view;

    public AppLauncher (Context context, AttributeSet attrs)
    {
        super (context, attrs);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService (Service.LAYOUT_INFLATER_SERVICE);
        this.view = inflater.inflate (R.layout.widget_applauncher, null, false);

        TypedArray styleAttrs = context.getTheme ().obtainStyledAttributes (attrs, R.styleable.AppLauncher, 0, 0);
        this.name = styleAttrs.getString (R.styleable.AppLauncher_label);
        this.description = styleAttrs.getString (R.styleable.AppLauncher_description);
        this.special = styleAttrs.getBoolean (R.styleable.AppLauncher_special, false);
        this.icon = new AppIcon (styleAttrs.getDrawable (R.styleable.AppLauncher_icon));
    }

    ...
}


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

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffff9243"
        android:layout_margin="6dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/launcher_icon_bg">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imgIcon"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AppLauncher">
        <attr name="label" format="string" />
        <attr name="description" format="string" />
        <attr name="icon" format="integer" />
        <attr name="special" format="boolean" />
    </declare-styleable>
</resources>

沒關系,但是Java代碼最好只包含邏輯部分。 我不知道如何指定自定義視圖/窗口小部件應使用哪個布局XML文件。 誇大布局可能會加載它。 但是, inflate ()方法需要第二個和第三個參數。 每個例子我能找到顯示this是作為第二個參數傳遞,但第二個參數應該是一個ViewGroup ,而this擴展View 我從哪里神奇地獲得了這個必需的ViewGroup對象? 傳遞nullfalse不會引發錯誤,但是它也不會做其他任何事情(運行應用程序時什么都沒有顯示)。

使用以下代碼在自定義視圖中將xml膨脹

    public AppLauncher (Context context, AttributeSet attrs)
    {
             super (context, attrs);
             LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Service.LAYOUT_INFLATER_SERVICE);
             View parent = inflater.inflate(R.layout.home_activity, this);
    }

從您的源代碼看來,您想要創建一個ViewGroup因此您需要擴展例如適當的類LinearLayout。 接下來,您可以照常創建一個xml文件,並在自定義ViewGroup構造函數中對其進行充氣:

 LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.ur_view, this);

然后,您可以通過以下方式將自定義視圖注入任何布局中:

<package.def.AppLauncher .../>

要么:

<View 
      class="package.defAppLauncher"
/View>

感謝Pr38y和eldjon的LayoutInflater
最后,最大的問題是我需要擴展LinearLayout而不是View

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM