如何在指定位置的图像上放置文本框?

时间:2012-07-27 08:58:52

标签: android imageview android-edittext

我想在Java代码中动态地将TextBox放在图像上。这是我的代码:

    ImageView image2 = new ImageView(this);       
    image2.setPadding(25, 25, 0, 0);  
    image2.setId(2001);
    image2.setImageResource(R.drawable.img);
    LayoutParams layoutParams = new   LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    image2.setLayoutParams(layoutParams);  
    linear.addView(image2);

我的形象:

enter image description here

我如何在这张图片上放置一个textview ???帮我解决.. !!!

3 个答案:

答案 0 :(得分:0)

将ImageView和EditText包装在RelativeLayout中(wrap_content用于高度和宽度)。首先尝试使用布局编辑器,然后获取值以编程方式设置它。

答案 1 :(得分:0)

尝试此代码作为演示:

        //Main Rel_Layout
    RelativeLayout scrollHolder = new RelativeLayout(this);
    scrollHolder.setId(++myid);
    RelativeLayout.LayoutParams scrollHolderParams = new RelativeLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    scrollHolder.setLayoutParams(scrollHolderParams);
    scrollHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

    //  Image Holder Layout
    RelativeLayout imgHolder = new RelativeLayout(this);
    imgHolder.setId(++myid);
    RelativeLayout.LayoutParams imgHolderParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    imgHolder.setLayoutParams(imgHolderParams);
    imgHolder.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
    //imgHolder.setBackgroundColor(Color.BLUE);
    imgHolder.setLayoutParams(imgHolderParams);

        //  Image Object
    ImageView image2 = new ImageView(this);       
    image2.setId(++myid);
    image2.setBackgroundColor(Color.BLUE);
    //int resId = HomePage.this.getResources().getIdentifier("img2", "drawable", HomePage.this.getPackageName());
    image2.setImageResource(R.drawable.img2);
    //  set image to its holder
    imgHolder.addView(image2);
    //  set imgHolder to main Layout
    scrollHolder.addView(imgHolder);
    //   set main layout as content-view
    setContentView(scrollHolder);
// this will sure help you.

较旧的 最简单的方式是: [1]使用

相对布局中添加EditText

layout-center-horizo​​ntal = true和layout-center-verticall = true

[2]在xml中设置其Visibility = GONE

[3]在代码文件中取其对象,并设置其“text-value” Visibility = VIEW.Visible

答案 2 :(得分:0)

你为什么试着这样做我的java?认为你应该看看FrameLayoutz-index z-index是由添加项目的顺序定义的:

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/my_drawable"
    android:scaleType="fitCenter"
    />
<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:padding="5dp"
    />