2012-07-27 51 views
0

我想要在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 ???幫助我出PLZ .. !!!

+0

使用疊加,繪製與圖像/帆布donig這個過程中工作 – 2012-07-27 09:01:33

回答

0

將您的ImageView和EditText包裹在一個RelativeLayout(高度和寬度的wrap_content)中。首先嚐試使用佈局編輯器並獲取值以編程方式進行設置。

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,與

佈局中心-horizo​​ntal = true和layout-center-verticall = true

[2]在XML設定其可見性= GONE

[3]在代碼文件採取它的對象,並設置其「文本值」能見度= VIEW.Visible

+0

即時動態不手動.. !!!我不會在XML文件中編輯...我想在java代碼中執行 – Amitabha 2012-07-27 09:07:20

+0

不要使用線性佈局,你需要**相對佈局** ... – 2012-07-27 09:09:28

+0

謝謝你...虐待! – Amitabha 2012-07-27 09:13:21

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" 
    /> 

+0

我不會編輯我的XML文件..!我想在Java文件..它完成.. – Amitabha 2012-07-31 07:41:20

相關問題