在android中相对布局中动态添加两个已创建按钮的视图

时间:2016-06-23 11:31:48

标签: android

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="evip.gohybrid.com.evip.ShowBarcode">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnaddtoprofile"
    android:text="Add to Profile"
    android:textStyle="bold"
    android:textSize="20dp"
    android:layout_marginTop="10dp"
    android:gravity="center"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btncancel"
    android:text="Cancel"
    android:textStyle="bold"
    android:textSize="20dp"
    android:layout_below="@+id/btnaddtoprofile"
    android:gravity="center"/>

这是我的代码:

Bitmap bitmap = null;

    ImageView iv = new ImageView(this);

    try {

        bitmap = encodeAsBitmap(code, BarcodeFormat.CODE_128, 600, 300);
        iv.setImageBitmap(bitmap);

    } catch (WriterException e) {
        e.printStackTrace();
    }
    relativelayout.addView(iv);

    //barcode text
    TextView tv = new TextView(this);
    tv.setGravity(Gravity.CENTER_HORIZONTAL);
    tv.setText(code);
    tv.setTextSize(25);

relativelayout.addView(TV);

我想在顶部添加imageview,然后动态地添加textview,然后在我的布局文件中添加两个按钮....我怎么能这样做?任何人都可以给我建议..?

1 个答案:

答案 0 :(得分:1)

你可以这样做:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="100" >
    <LinearLayout
        android:id="@+id/your_dinamyc_layout"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70"
       >
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">

       <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btnaddtoprofile"
        android:text="Add to Profile"
        android:textStyle="bold"
        android:textSize="20dp"
        android:layout_marginTop="10dp"
        android:gravity="center"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/btncancel"
            android:text="Cancel"
            android:textStyle="bold"
            android:textSize="20dp"
            android:layout_below="@+id/btnaddtoprofile"
            android:gravity="center"/>
    </LinearLayout>
</LinearLayout>

我没有添加dinamycally一个imageview我添加了两个textviews而不是:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout your_dinamic_layout=(LinearLayout)findViewById(R.id.your_dinamyc_layout);
        TextView tv = new TextView(this);
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setText("code");
        tv.setTextSize(25);
        your_dinamic_layout.addView(tv);
        TextView tv2 = new TextView(this);
        tv2.setGravity(Gravity.CENTER_HORIZONTAL);
        tv2.setText("code 2");
        tv2.setTextSize(30);
        your_dinamic_layout.addView(tv2);
    }
}

结果是:

enter image description here