【问题标题】:Create custom Android view/widget创建自定义 Android 视图/小部件
【发布时间】:2014-08-21 07:10:47
【问题描述】:

我正在尝试创建一个可以在我的活动布局中使用的自定义小部件。我为扩展 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 文件。 Inflating 布局大概会加载它。不过,inflate () 方法需要第二个和第三个参数。我能找到的每个示例都显示this 作为第二个参数传递,但第二个参数应该是ViewGroup,而this 扩展View。我从哪里神奇地得到这个必需的ViewGroup 对象?传递 nullfalse 不会引发错误,但它也不会做任何其他事情(运行应用程序时没有任何显示)。

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    使用以下代码在自定义视图中扩充 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);
        }
    

    【讨论】:

    • ctrl+shift+o 导入你的 R 文件。
    • 我认为 Import Class 会导致导入 android.R 并且不知何故我自己的 R.java 文件无法编译。我应该检查一下。对不起,谢谢:)
    • 这个答案不正确。 this 不是有效参数,因为 thisView,而 ViewGroup 是预期的。那么我应该从哪里得到这个ViewGroup
    • 扩展 ViewGroup 类而不是 View 正如你已经想通的那样。
    【解决方案2】:

    从您的源代码看来,您想要创建一个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>
    

    【讨论】:

    • 我有点不清楚第二个参数是什么,因为this 不起作用(因为它指的是我的AppLauncher 类)。根据文档,它应该是ViewGroup,这并不能真正告诉我很多。
    • 当我将视图/小部件添加到我的布局时,它只是空的/不可见的,所以有些地方不对。
    • 这个答案不正确。将null 作为参数传递不会将视图添加到其父级,导致它不显示。
    • 如果你扩展一个像 LinearLayout 这样的 ViewGroup 来解决你的问题
    【解决方案3】:

    感谢 Pr38y 和 eldjon 提供LayoutInflater
    最后最大的问题是我需要扩展LinearLayout而不是View

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-08
      • 2023-03-20
      • 2019-11-20
      相关资源
      最近更新 更多