1

Добрый день. Помогите с решением такого вопроса. Есть разметка:

<?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: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>
Вот мой View класс:
    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;
        }
        invalidate();
    }

    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);
    }

}
в Activity на OnClick нужно добавить мой BarCodeView в LinearLayout(@+id/linearCodeView). Но я его после добавления не вижу. Помогите с кодом.
вот мой Activity: 
public class GenBarCodeActivity extends Activity {
LinearLayout ll;
Button getBarCode;
ImageView imBarImage;
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);
    String EANTEXT = edBarText.getText().toString();
    ImageView old = (ImageView) ll.findViewById(ID);
    if (old != null) {
      ((LinearLayout) old.getParent()).removeViewInLayout(old);
    }
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                        android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    ll.addView(Barview, params);
    }
    });
    setBarCode();
    fillLinear();
}
private void fillLinear(){
    ll = (LinearLayout)rl.findViewById(R.id.linearCodeView);
    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);
        Barview.setBarCodeText(EANTEXT);
    }
}

1 ответ 1

1

Думаю эта ссылка Вам поможет. Вам надо добавлять с помощью LayoutInflater.

Ваш ответ

Нажимая «Отправить ответ», вы соглашаетесь с условиями пользования и подтверждаете, что прочитали политику конфиденциальности.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.