RemoteViews在View类中不起作用

时间:2013-04-18 17:00:30

标签: android view remoteview

我在View类中制作自定义UI,我在其中制作了RemoteViews,但它无效。

查看课程

public class CustomeView extends View {

    Bitmap icon;
    float left=0;
    RemoteViews rs;
    int width=0,height=0;
    float def_value=0;
    boolean unlock=false;

    public CustomeView(Context context, AttributeSet attrs) {
        super(context, attrs);
        icon=BitmapFactory.decodeResource(getResources(), R.drawable.icon);
        rs=new RemoteViews(context.getPackageName() ,R.layout.custom_ui);

        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        width = display.getWidth();
        height = display.getHeight();

        left=width/2;
        def_value=left;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        Paint a=new Paint();
        a.setTextSize(10);

        canvas.drawBitmap(icon, left, 0, null);
        canvas.drawText(String.valueOf(unlock), 0, 110, a);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
           setMeasuredDimension(width, height);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction()==MotionEvent.ACTION_MOVE){
            left=event.getX()-(icon.getWidth()/2);
            isUnlock();
        }
        else if (event.getAction()==MotionEvent.ACTION_UP) {
            if(def_value!=left){
                left=def_value;
            }
        }
        invalidate();
        return true;
    }

    protected void isUnlock() {
        if(left<=20){           
            unlock=true;
            rs.setTextViewText(R.id.tvStatus, "True");
        }
        if(left>=width-20){
            unlock=true;
            rs.setTextViewText(R.id.tvStatus, "True");
        }
    }
}

custom_ui.xml布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.helloworl.CustomeView 
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <TextView
        android:id="@+id/tvStatus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="154dp"
        android:text="False" />

</RelativeLayout>

RemoteViews在isUnlock方法中更改TextView的值但它不起作用。

1 个答案:

答案 0 :(得分:3)

您不能将自定义视图与RemoteViews一起使用,因为其他进程无法访问您的Java代码。