2014-06-06 3 views
1

Description de ::Programatically ajouter des vues sans élément XML dans Android

  • Sur clic de bouton Je veux ajouter un textview dynamique sur l'exécution
  • Je ne veux pas utiliser un widget TextView (à partir de xml)
  • Je veux générer des textviews par programme
  • Est-ce possible?
  • Si oui, comment?

MainActivity.java

public class MainActivity extends Activity { 

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



     button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 


      } 
     }); 
    } 
} 

activity_main.xml

<RelativeLayout 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:id="@+id/Container" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/checkBox1" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="67dp" 
     android:text="Button" /> 

</RelativeLayout> 

Répondre

1

Quelque chose comme ceci:

// Create the TextView 
TextView textView = new TextView(this); 

// Create some parameters 
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT, 
              LayoutParams.WRAP_CONTENT); 

p.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 

// Get your relative layout by its id 
RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.Container); 

// Add the newly created TextView to the layout 
relativeLayout.addView(textView, p); 
1

Juste

button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       LinearLayout layout = (LinearLayout) findViewById(R.id.parent_layout); 
       TextView textView = new TextView(MainActivity.this); 
       layout .addView(textView); 
      } 
     }); 
1

Vous pouvez créer un textView en utilisant le contexte de votre activité.

exemple:

RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.Container); 


button1.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
TextView tv = new TextView(MainActivity.this); 
       relativeLayout.addView(tv,lp); 
      } 
     }); 
1

exemple simple, y compris params de mise en page (largeur, hauteur, etc.):

// instantiate the new view - int his example, a textview 
    TextView label=new TextView(context); 
    label.setText(labelText); 

    // new LayoutParams, specify the height as WRAP_CONTENT 
    // width is 0 in this example as we are using the layout weight (2) 
    LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(
      0, LayoutParams.WRAP_CONTENT,2 
    ); 

    // margins 
    lp.rightMargin=5; 
    lp.leftMargin=5; 

    // apply the layout params to this view 
    label.setLayoutParams(lp); 

    // finally, add the view to the container view 
    content.addView(label); 
1

TextView dans un LinearLayout:

LinearLayout layout = new LinearLayout(this); 
TextView textview = new TextView(this); 
textview.setText("Hellow, I'm a TextView!"); 
layout.addView(textview); 
this.setContentView(layout); 
Questions connexes