【问题标题】:Custom Widget composed of other existing views由其他现有视图组成的自定义小部件
【发布时间】:2012-09-09 10:50:33
【问题描述】:

我想创建一个具有以下布局的小部件:

<LinearLayout orientation="horizontal">
<CheckBox />
<EditText />
<SeekBar />
</LinearLayout>

当搜索栏被移动时,它应该选中复选框并使用当前搜索栏值更新编辑框。

我在应用程序中一遍又一遍地使用类似的东西,并且厌倦了重新实现相同的东西。

将所有这些封装到自己的类中以供重用的最佳方法是什么?


public class SeekChoice extends LinearLayout {



    public SeekChoice(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        LinearLayout ll = (LinearLayout) inflater.inflate(R.id.widget_item, null);
        this.addView(ll);
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
    android:id="@+id/widget_item"
    >


    <CheckBox
        android:id="@+id/checkBox_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Item Name" />



    <EditText
        android:id="@+id/editText_item_count"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:ems="10" >

    </EditText>


    <SeekBar
        android:id="@+id/seekBar_item_count"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

然后在我的布局中输入:

<com.example.lobsternav.widget.SeekChoice
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>    

例外情况如下:

09-09 20:23:08.759: E/AndroidRuntime(21888): FATAL EXCEPTION: main
09-09 20:23:08.759: E/AndroidRuntime(21888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lobsternav/com.example.lobsternav.MarkActivity}: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.os.Looper.loop(Looper.java:137)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.main(ActivityThread.java:4745)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.reflect.Method.invokeNative(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.reflect.Method.invoke(Method.java:511)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at dalvik.system.NativeStart.main(Native Method)
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: android.view.InflateException: Binary XML file line #344: Error inflating class com.example.lobsternav.widget.SeekChoice
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createView(LayoutInflater.java:596)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Activity.setContentView(Activity.java:1867)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at com.example.lobsternav.MarkActivity.onCreate(MarkActivity.java:85)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Activity.performCreate(Activity.java:5008)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-09 20:23:08.759: E/AndroidRuntime(21888):    ... 11 more
09-09 20:23:08.759: E/AndroidRuntime(21888): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at java.lang.Class.getConstructor(Class.java:431)
09-09 20:23:08.759: E/AndroidRuntime(21888):    at android.view.LayoutInflater.createView(LayoutInflater.java:561)
09-09 20:23:08.759: E/AndroidRuntime(21888):    ... 24 more

疯狂...如果我没有定义以下构造函数,它会引发异常:

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


}

我猜LinearLayout的所有构造函数都需要定义,需要调用超级构造函数?

【问题讨论】:

    标签: android android-widget


    【解决方案1】:

    终于有这个工作了。

    我的 Java 代码:

    package com.example.lobsternav.widget;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import com.example.lobsternav.R;
    
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.util.AttributeSet;
    import android.view.LayoutInflater;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;
    import android.widget.CompoundButton.OnCheckedChangeListener;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.SeekBar;
    import android.widget.RelativeLayout.LayoutParams;
    import android.widget.SeekBar.OnSeekBarChangeListener;
    
    public  class SeekChoice extends LinearLayout implements OnSeekBarChangeListener, OnCheckedChangeListener{
    
        CheckBox checkbox = null;
        EditText editBox =null;
        SeekBar seekbar = null;
    
        boolean isChecked = false;
        int curValue = 0;
        List <String> mappings = new ArrayList<String>();
        int startRange = 0;
        int endRange = 50;
        int defaultValue = 0;
    
        private void init(Context context)
        {
            this.setOrientation(LinearLayout.HORIZONTAL);
            LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
    
            LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.item_widget, null);
            layout.setOrientation(LinearLayout.HORIZONTAL);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
    
    
            checkbox = (CheckBox)layout.findViewById(R.id.checkBox_item_name);
            checkbox.setOnCheckedChangeListener(this);
            //this.addView(checkbox);
    
            editBox = (EditText)layout.findViewById(R.id.editText_item_count);
            editBox.setEnabled(false);
            editBox.setText(getMappedValue(defaultValue));
    
            int ems = (new Integer(endRange)).toString().length()+1;
            editBox.setEms(ems);
            //this.addView(editBox);
    
            seekbar = (SeekBar)layout.findViewById(R.id.seekBar_item_count);
            seekbar.setMax(endRange);
            seekbar.setProgress(defaultValue);
            //seekbar.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
            seekbar.setOnSeekBarChangeListener(this);
            //this.addView(seekbar);    
            this.addView(layout);
    
        }
    
    
        public void readAttributes(Context context, AttributeSet attrs)
        {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SeekChoice);
            this.startRange = a.getInteger(R.styleable.SeekChoice_startRange, 0);
            this.endRange = a.getInteger(R.styleable.SeekChoice_endRange, 100);
            this.defaultValue = a.getInteger(R.styleable.SeekChoice_defaultValue, 0);
            this.isChecked = a.getBoolean(R.styleable.SeekChoice_isChecked, false);
            a.recycle();
        }
    
        public SeekChoice(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            readAttributes( context,  attrs);
            init(context);
    
        }
    
        public SeekChoice(Context context, AttributeSet attrs) {
            super(context, attrs);
            readAttributes( context,  attrs);
            init(context);
    
        }
        public SeekChoice(Context context) {
            super(context);
            init(context);
        }
    
        public SeekChoice(Context context,int startRange, int endRange, int defaultValue) {
            super(context);
            init(context);
        }
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) 
        {
            this.editBox.setText("" + getMappedValue(progress));
    
            if (progress > 0)
            {
                this.checkbox.setChecked(true);
                isChecked = true;
            }
    
        }
    
    
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
    
    
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
    
        }
    
    
    
    
        public int getStartRange() {
            return startRange;
        }
    
    
        public void setStartRange(int startRange) {
            this.startRange = startRange;
        }
    
    
        public int getEndRange() {
            return endRange;
        }
    
    
        public void setEndRange(int endRange) {
            this.endRange = endRange;
        }
    
    
        public int getDefaultValue() {
            return defaultValue;
        }
    
    
        public void setDefaultValue(int defaultValue) {
            this.defaultValue = defaultValue;
        }
    
    
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {
    
            this.isChecked = isChecked;
    
            if (isChecked == false)
            {
                this.seekbar.setProgress(0);
            }
    
            this.editBox.setText("" + getMappedValue(0));
        }
    
    
    
        public String getMappedValue(int current)
        {
            if (current < this.getMappings().size())
            {
                return this.getMappings().get(current);
            }
    
    
    
            return "" + current;
    
    
        }
    
        public List<String> getMappings() {
            return mappings;
        }
    
    
        public void setMappings(List<String> mappings) {
            this.mappings = mappings;
        }
    
        public void addMappings(String mapping) {
            this.mappings.add(mapping);
        }
        public boolean isChecked() {
            return isChecked;
        }
    
    
        public String getCurrentValue()
        {
            return this.editBox.getText().toString();
        }
        public void setChecked(boolean isChecked) {
            this.isChecked = isChecked;
        }
    
    
    }
    

    我在构造函数中膨胀的布局:

    <?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="wrap_content"
        android:orientation="vertical" 
        android:id="@layout/item_widget">
    
       <CheckBox
            android:id="@+id/checkBox_item_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Item Name" />
    
    
    
        <EditText
            android:id="@+id/editText_item_count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:enabled="false"
            android:ems="10" >
    
        </EditText>
    
    
        <SeekBar
            android:id="@+id/seekBar_item_count"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    </LinearLayout>
    

    /res/values/attr.xml(用于将自定义属性传递给小部件):

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
           <declare-styleable name="SeekChoice">
                <attr name="startRange" format="integer"/>
                <attr name="endRange" format="integer"/>
                <attr name="defaultValue" format="integer"/>                
                <attr name="isChecked" format="boolean"/>       
    
    
    
    
           </declare-styleable>
    
    </resources> 
    

    我在其中包含小部件的布局文件:

        <RelativeLayout android:layout_width="fill_parent"
            android:layout_height="match_parent" 
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:custom="http://schemas.android.com/apk/res/com.example.lobsternav">
    .....
    .....
    
    <com.example.lobsternav.widget.SeekChoice
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    custom:defaultValue="10"
    />
    
    .....
    .....
    
    </RelativeLayout>
    

    浪费了我的时间,所以我分享给大家:

    1)。确保添加要覆盖的类的所有构造函数

    2)。命名空间(在我的示例中为“xmlns:custom="http://schemas.android.com/apk/res/com.example.lobsternav")必须是您的 R 所在的包名称,而不是小部件驻留。

    3)。在 attr.xml 文件中,标签 declare-styleable 不会在 eclipse xml 编辑器中自动完成,因此它看起来不像是一个有效的标签 - 但它是。我读了另外 2 篇文章说要使用它,但是 android eclipse 插件没有将它显示为资源下的有效标签,所以我几乎没有尝试使用该标签。

    【讨论】:

      【解决方案2】:

      选项 #1:不要将其打包到自定义小部件中,而是打包到片段中。

      选项#2:创建LinearLayout 的子类,将其方向设置为水平并将布局文件膨胀到自身中,其中包含您上面的内容,并用merge 标记替换LinearLayout(或将其子类直接添加到Java 代码,如果您喜欢并且不需要布局级别的可定制性)。然后LinearLayout 可以连接其子级中的事件侦听器等等。

      【讨论】:

      • 我尝试了第二个选项,但遇到了一些异常。我将在原帖中发布代码和异常。
      • @George:这是一个自定义 LinearLayout 的示例,如果这就是您的意思:stackoverflow.com/a/12318422/115145
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 2016-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多