自定义控件Android

奶酪

我已经通过了Android上的几门在线课程。并开始了我的第一个项目。目前,我坚持创建自己的Widget。

我找不到有关如何覆盖默认控件样式或如何将默认控件分组为自定义控件的任何信息。问题是:

当我们创建一个Activity扩展时,有一个onCreated方法,用于指出要使用的XML布局。我认为应该以相同的方式创建自定义控件。

例如,我要创建一个控件,该控件将具有ImageView和一些按钮,这些按钮的类型将取决于来自服务器的数据(电子邮件按钮,skype按钮或任何其他按钮)

所以我创建了一个类:

public class InteractiveHeaderControl extends View {

    public InteractiveHeaderControl(Context context)
    {
        super(context);
    }
}

我创建了一个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true"
    android:text="TEST"
    android:layout_marginLeft="20dp"/>
<LinearLayout
    android:id="@+id/button_stack"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true">

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <Button
        android:layout_width="50dp"
        android:layout_height="50dp" />
    <Button
        android:layout_width="50dp"
        android:layout_height="50dp" />
</LinearLayout>
</RelativeLayout>

所以问题是,如何在运行时将此布局应用于控件并填充button_stack?

例如,在代码中,我想创建此控件,例如:

InteractiveHeaderControl control = new InteractiveHeaderControl(List<button>);

并在控件构造函数中处理传递的参数。

迈克利斯·坎尼普斯(Mikelis Kaneps)

使用布局而不是视图。这是一个例子:

public class DefaultHeading extends FrameLayout {        

    public DefaultHeading(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.default_heading, this, true);

        addView(view);
    }

如果要使用一些属性:

将此添加到res-> values-> attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="awesomeedit">
        <attr name="regex" format="string"/>
        <attr name="name" format="string"/>
        <attr name="hint" format="string"/>
        <attr name="error" format="string"/>
        <attr name="required" format="boolean"/>
        <attr name="link" format="string"/>
        <attr name="email" format="boolean"/>
        <attr name="number" format="boolean"/>
        <attr name="password" format="boolean"/>
    </declare-styleable>
</resources>

在布局文件中使用它:

<yourpackage.DefaultHeading
... 

android:layout_width="match_parent"
android:layout_height="wrap_content"
app:error="@string/not_valid_email_msg"
...
>

获取值:

 public class DefaultHeading extends FrameLayout {  



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


 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.awesomeedit, 0, 0);
        String name;
        String regex;
        String hint;
        String error;
        String link;
        try {
            name = ta.getString(R.styleable.awesomeedit_name);
            regex = ta.getString(R.styleable.awesomeedit_regex);
            hint = ta.getString(R.styleable.awesomeedit_hint);
            error = ta.getString(R.styleable.awesomeedit_error);
            link = ta.getString(R.styleable.awesomeedit_link);
            required = ta.getBoolean(R.styleable.awesomeedit_required, false);
            email = ta.getBoolean(R.styleable.awesomeedit_email, false);
            password = ta.getBoolean(R.styleable.awesomeedit_password, false);
            nummber = ta.getBoolean(R.styleable.awesomeedit_number, false);
        } finally {
            ta.recycle();
        }
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.default_heading, this, true);

        addView(view);
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章