2016-06-23 2 views
-1

das ist mein Layout:add Ansicht dynamisch über zwei bereits erstellte Schaltfläche in relativ Layout in 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"/> 

dies ist mein Code:

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

ich möchte imageview oben und dann textview dynamisch hinzufügen und dann die zwei taste die ist in meiner layout-datei .... wie kann ich es tun ...? kann mir jemand einen Vorschlag geben ..?

+0

können Sie mir antworten plz ..? –

Antwort

1

Sie können es auf diese Weise tun:

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

Ich habe nicht dinamycally ein Imageview Ich habe zwei Textviews statt hinzugefügt:

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

und das Ergebnis ist:

enter image description here

Verwandte Themen