我想在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);
我的形象:
我如何在这张图片上放置一个textview ???帮我解决.. !!!
答案 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]使用
在相对布局中添加EditTextlayout-center-horizontal = true和layout-center-verticall = true
[2]在xml中设置其Visibility = GONE
[3]在代码文件中取其对象,并设置其“text-value”和 Visibility = VIEW.Visible
答案 2 :(得分:0)
你为什么试着这样做我的java?认为你应该看看FrameLayout
和z-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"
/>