以编程方式在drawable下创建editText

时间:2013-12-27 22:16:53

标签: java android xml android-edittext android-drawable

我有一个editText:@ + id / editText1我想在我的onClick方法之后在R.drawable.sqwhite下面实例化

我的问题是drawable不是通过XML创建的 - 而且我不确定如何让我的editText1直接出现在R.drawable.sqwhite下面

JAVA:

public void onClickAddImage(View v) {
    addNewImageToScreen();
}

public void addNewImageToScreen() {
    int resourceId = R.drawable.sqwhite;
    addNewTextToScreen();
    int m = mImageCount % 3;

    if (m == 1)
        resourceId = R.drawable.sqdrk;

    else if (m == 2)
        resourceId = R.drawable.sqwhite;
    addNewImageToScreen(resourceId);

}

private void addNewTextToScreen() {
    // TODO Auto-generated method stub

    et.setVisibility(View.VISIBLE);
    if (isErase) {
        tx.setText(et.getText().toString());
    } else {
        tx.setText("");
        et.setVisibility(View.GONE);
    }
    isErase = !isErase;
}


public void addNewImageToScreen(int resourceId) {
    if (mLastNewCell != null)
        mLastNewCell.setVisibility(View.GONE);
    et.setVisibility(View.VISIBLE);
    FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
    if (imageHolder != null) {
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
                Gravity.CENTER);
        ImageCell newView = new ImageCell(this);
        newView.setImageResource(resourceId);
        imageHolder.addView(newView, lp);
        newView.mEmpty = false;
        newView.mCellNumber = -1;
        mLastNewCell = newView;
        mImageCount++;

        // Have this activity listen to touch and click events for the view.
        newView.setOnClickListener(this);
        newView.setOnLongClickListener(this);
        newView.setOnTouchListener(this);

    }
}

XML:

<?xml version="1.0" encoding="utf-8"?>
<com.example.project.DragLayer xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:launcher="http://schemas.android.com/apk/res/com.android.launcher"
    android:id="@+id/drag_layer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:weightSum="1.0" >

            <GridView
                android:id="@+id/image_grid_view"
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:layout_weight="0.8"
                android:background="@color/grid_background"
                android:gravity="center"
                android:horizontalSpacing="2dip"
                android:numColumns="@integer/num_columns"
                android:stretchMode="columnWidth"
                android:verticalSpacing="2dip" />

            <LinearLayout
                android:id="@+id/bottom_part"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@android:color/black"
                android:layout_centerHorizontal="true"
                android:layout_weight="0.2"
                android:orientation="horizontal"
                android:weightSum="1.0" >

                <Button
                    android:id="@+id/button_add_image"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:onClick="onClickAddImage"
                    android:text="Add image" />

                <com.example.project.DeleteZone
                    android:id="@+id/delete_zone_view"
                    android:layout_width="60dp"
                    android:layout_height="60dp"
                    android:layout_gravity="center"
                    android:src="@drawable/delete_zone" />

                <FrameLayout
                    android:id="@+id/image_source_frame"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_weight="0.5" >

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >



                        <EditText
                            android:id="@+id/editText1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <requestFocus />
                        </EditText>

                        <TextView
                            android:id="@+id/textView1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                             android:layout_below="@+id/editText1"
                              android:textColor="@android:color/white"
                            android:text="" />
                    </RelativeLayout>
                </FrameLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</com.example.project.DragLayer>

截图:

enter image description here

0 个答案:

没有答案