2013-06-18 66 views
0

幫我解決我的問題。有一個主佈局RelativeLayout它是Linearlayout,我想以編程方式添加視圖。這是我的代碼。的Android添加視圖中的RelativeLayout到的LinearLayout按鈕點擊

public class GenBarCodeActivity extends Activity { 
      BarCodeView Barview = null; 
      LinearLayout.LayoutParams layoutParams; 
      LinearLayout ll; 
      RelativeLayout rl; 
      Button getBarCode; 
      private static final int ID = 34646456; 
      @Override 
      public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.activity_barcode); 
      getBarCode = (Button)findViewById(R.id.btnGetBarCode); 
      getBarCode.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
      // TODO Auto-generated method stub 
      ll = (LinearLayout)findViewById(R.id.linearCodeView); 
      EANTEXT = edBarText.getText().toString(); 
      ImageView old = (ImageView) ll.findViewById(ID); 
      if (old != null) { 
       ((LinearLayout) old.getParent()).removeViewInLayout(old); 
      } 
      layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      ll.addView(Barview, layoutParams); 
      rl.invalidate(); 
      } 
       }); 
      fillLinear(); 
      setBarCode(); 
     } 
     private void fillLinear(){ 
       rl = (RelativeLayout)findViewById(R.id.layout_barcode); 
       ll = (LinearLayout)findViewById(R.id.linearCodeView); 
       layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
       ImageView imBar = new ImageView(this); 
       Bitmap imbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.barcode); 
       imBar.setLayoutParams(layoutParams); 
       imBar.setImageBitmap(imbitmap); 
       imBar.setId(ID); 
       ll.setBackgroundColor(Color.TRANSPARENT); 
       ll.addView(imBar); 
      } 
      private void setBarCode(){ 
       Barview = new BarCodeView(GenBarCodeActivity.this); 
       Barview.setBarCodeNum(0); 
      } 
     } 

這是我的看法

public class BarCodeView extends View { 
     private static int numcode = 0; 
    private static String codedata = null; 
    public BarCodeView(Context context) { 
     super(context); 
    } 
    public void setBarCodeText(String text){ 
     this.codedata = text; 
    } 
    public String getBarCodeText(){ 
     return this.codedata; 
    } 
    public void setBarCodeNum(int num){ 
     this.numcode = num; 
    } 
    public int getBarCodeNum(){ 
     return this.numcode; 
    } 
    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     switch (numcode) { 
     // ean 13 
     case 0: 
      try{ 
       showEAN13(canvas); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     break; 
     } 
    } 
    private static void showEAN13(Canvas canvas) throws Exception { 
     EAN13 barcode = new EAN13(); 
     /* 
      EAN 13 Valid data char set: 
       0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (Digits) 

      EAN 13 Valid data length: 12 digits only, excluding the last checksum digit 
     */ 
     barcode.setData(codedata); 
     // for EAN13 with supplement data (2 or 5 digits) 
     /* 
     barcode.setSupData("12"); 
     // supplement bar height vs bar height ratio 
     barcode.setSupHeight(0.8f); 
     // space between barcode and supplement barcode (in pixel) 
     barcode.setSupSpace(15); 
     */ 
     // Unit of Measure, pixel, cm, or inch 
     barcode.setUom(IBarcode.UOM_PIXEL); 
     // barcode bar module width (X) in pixel 
     barcode.setX(2f); 
     // barcode bar module height (Y) in pixel 
     barcode.setY(90f); 
     // barcode image margins 
     barcode.setLeftMargin(10f); 
     barcode.setRightMargin(10f); 
     barcode.setTopMargin(10f); 
     barcode.setBottomMargin(10f); 
     // barcode image resolution in dpi 
     barcode.setResolution(72); 
     // disply barcode encoding data below the barcode 
     barcode.setShowText(true); 
     // barcode encoding data font style 
     barcode.setTextFont(new AndroidFont("Arial", Typeface.NORMAL, 10)); 
     // space between barcode and barcode encoding data 
     barcode.setTextMargin(6); 
     barcode.setTextColor(AndroidColor.black); 
     // barcode bar color and background color in Android device 
     barcode.setForeColor(AndroidColor.black); 
     barcode.setBackColor(AndroidColor.white); 
     /* 
     specify your barcode drawing area 
     */ 
     RectF bounds = new RectF(120, 120, 0, 0); 
     barcode.drawBarcode(canvas, bounds); 
    } 
} 

,這裏是我的xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout_barcode" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f0e0a2" 
    android:orientation="vertical" > 

    <ImageView 
     android:id="@+id/closeBarCode" 
     android:layout_width="30dip" 
     android:layout_height="30dip" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/txtInfo" 
     android:layout_marginRight="3dp" 
     android:src="@drawable/delete" /> 

    <TextView 
     android:id="@+id/txtInfo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="3dp" 
     android:layout_marginTop="3dp" 
     android:text="@string/barcode_title" 
     android:textColor="#000" 
     android:textSize="20dip" /> 

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/closeBarCode" > 

     <TextView 
      android:id="@+id/txtEAN" 
      android:layout_width="160dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/barcode_ean" 
      android:textColor="#000" 
      android:textSize="20dip" /> 

     <Spinner 
      android:id="@+id/spEAN" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/txtInfo" 
     android:layout_below="@+id/linearLayout1" > 

     <TextView 
      android:id="@+id/txtCodeColor" 
      android:layout_width="160dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/barcode_color" 
      android:textColor="#000" 
      android:textSize="20dip" /> 

     <Spinner 
      android:id="@+id/spCodeColor" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearLayout2" 
     android:layout_below="@+id/linearLayout2" > 

     <TextView 
      android:id="@+id/txtCodeBackColor" 
      android:layout_width="160dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/barcode_back_color" 
      android:textColor="#000" 
      android:textSize="20dip" /> 

     <Spinner 
      android:id="@+id/spCodeBackColor" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearLayout4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/linearLayout3" > 

     <TextView 
      android:id="@+id/txtCodePosition" 
      android:layout_width="160dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center" 
      android:text="@string/barcode_position" 
      android:textColor="#000" 
      android:textSize="20dip" /> 

     <Spinner 
      android:id="@+id/spCodePosition" 
      android:layout_width="300dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/linearCodeView" 
     android:layout_width="300dip" 
     android:layout_height="200dip" 
     android:layout_below="@+id/edBarCode" 
     android:layout_marginRight="45dp" 
     android:layout_marginTop="25dip" 
     android:layout_toLeftOf="@+id/closeBarCode" 
     android:orientation="vertical" > 
    </LinearLayout> 

    <Button 
     android:id="@+id/btnSetBarCode" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/btnGetBarCode" 
     android:layout_alignBottom="@+id/btnGetBarCode" 
     android:layout_toLeftOf="@+id/closeBarCode" 
     android:text="@string/set_code" /> 

    <Button 
     android:id="@+id/btnGetBarCode" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginBottom="31dp" 
     android:text="@string/get_code" /> 

    <EditText 
     android:id="@+id/edBarCode" 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/linearCodeView" 
     android:layout_alignRight="@+id/linearCodeView" 
     android:layout_below="@+id/txtCodeText" 
     android:layout_marginTop="23dp" 
     android:ems="10" /> 

    <TextView 
     android:id="@+id/txtCodeText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/edBarCode" 
     android:layout_below="@+id/txtInfo" 
     android:layout_marginRight="86dp" 
     android:text="@string/code_text" 
     android:textColor="#000" 
     android:textSize="20dip" /> 
</RelativeLayout> 

我需要的LinearLayout 「@ + ID/linearCodeView」 插入我的BarCodeView。 添加LinearLayout後視圖不可見。

回答

0
RelativeLayout.LayoutParams paramsBottom = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 
android.view.ViewGroup.LayoutParams.WRAP_CONTENT); 
rl.addView(ll, paramsBottom);  //add linearlayout in relativelayout 

編輯: -

LinearLayout.LayoutParams paramsBottom2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
LinearLayout.LayoutParams.WRAP_CONTENT); 
ll.addView(Barview, paramsBottom2);//add Barview in linearlayout 

OR

RelativeLayout.LayoutParams paramsBottom2 = new RelativeLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 
android.view.ViewGroup.LayoutParams.WRAP_CONTENT); 
rl.addView(Barview, paramsBottom2);//add Barview in relativelayout 
+0

我改你給的代碼。但BarView不可見。 – drak2000

+0

嘗試barview加入的LinearLayout或RelativeLayout的... – AnilPatel

相關問題