如何在Android中dynamic添加TextView到LinearLayout?

我尝试dynamic添加一个TextView到一个LinearLayout,如下面的代码,但是当我运行该应用程序时,它不会出现?

setContentView(R.layout.advanced); m_vwJokeLayout=(LinearLayout) this.findViewById(R.id.m_vwJokeLayout); m_vwJokeEditText=(EditText) this.findViewById(R.id.m_vwJokeEditText); m_vwJokeButton=(Button) this.findViewById(R.id.m_vwJokeButton); TextView tv=new TextView(this); tv.setText("test"); this.m_vwJokeLayout.addView(tv); 

有什么问题?

 LayoutParams lparams = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); TextView tv=new TextView(this); tv.setLayoutParams(lparams); tv.setText("test"); this.m_vwJokeLayout.addView(tv); 

您可以根据您的需要更改lparams

对于这个问题的未来观众来说,这是一个更一般的答案。 我们将做的布局如下:

在这里输入图像说明

方法1:将TextView添加到现有的LinearLayout

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dynamic_linearlayout); LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_example); // Add textview 1 TextView textView1 = new TextView(this); textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); textView1.setText("programmatically created TextView1"); textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom) linearLayout.addView(textView1); // Add textview 2 TextView textView2 = new TextView(this); LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.RIGHT; layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom) textView2.setLayoutParams(layoutParams); textView2.setText("programmatically created TextView2"); textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB linearLayout.addView(textView2); } 

请注意,对于LayoutParams您必须指定导入的布局types,如

 import android.widget.LinearLayout.LayoutParams; 

否则,您需要在代码中使用LinearLayout.LayoutParams

这里是xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_example" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff99ccff" android:orientation="vertical" > </LinearLayout> 

方法2:以编程方式创buildLinearLayout和TextView

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // NOTE: setContentView is below, not here // Create new LinearLayout LinearLayout linearLayout = new LinearLayout(this); linearLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); linearLayout.setOrientation(LinearLayout.VERTICAL); linearLayout.setBackgroundColor(0xff99ccff); // Add textviews TextView textView1 = new TextView(this); textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); textView1.setText("programmatically created TextView1"); textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB textView1.setPadding(20, 20, 20, 20); // in pixels (left, top, right, bottom) linearLayout.addView(textView1); TextView textView2 = new TextView(this); LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.RIGHT; layoutParams.setMargins(10, 10, 10, 10); // (left, top, right, bottom) textView2.setLayoutParams(layoutParams); textView2.setText("programmatically created TextView2"); textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); textView2.setBackgroundColor(0xffffdbdb); // hex color 0xAARRGGBB linearLayout.addView(textView2); // Set context view setContentView(linearLayout); } 

方法3:以编程方式将一个xml布局添加到另一个xml布局

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dynamic_linearlayout); LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.dynamic_linearlayout_item, null); FrameLayout container = (FrameLayout) findViewById(R.id.flContainer); container.addView(view); } 

这里是dynamic_linearlayout.xml:

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/flContainer" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> 

这里是dynamic_linearlayout_item.xml添加:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_example" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff99ccff" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ff66ff66" android:padding="20px" android:text="programmatically created TextView1" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffdbdb" android:layout_gravity="right" android:layout_margin="10px" android:textSize="18sp" android:text="programmatically created TextView2" /> </LinearLayout> 

我定制了更多@Suragch代码。 我的输出看起来

在这里输入图像说明

我写了一个方法来停止代码冗余。

 TextView createATextView(int layout_widh, int layout_height, int align, String text, int fontSize, int margin, int padding) { TextView textView_item_name = new TextView(this); // LayoutParams layoutParams = new LayoutParams( // LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); // layoutParams.gravity = Gravity.LEFT; RelativeLayout.LayoutParams _params = new RelativeLayout.LayoutParams( layout_widh, layout_height); _params.setMargins(margin, margin, margin, margin); _params.addRule(align); textView_item_name.setLayoutParams(_params); textView_item_name.setText(text); textView_item_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSize); textView_item_name.setTextColor(Color.parseColor("#000000")); // textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB textView_item_name.setPadding(padding, padding, padding, padding); return textView_item_name; } 

它可以被称为像

 createATextView(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT, subTotal.toString(), 20, 10, 20); 

现在你可以dynamic地将它添加到RelativeLayout中。 LinearLayout也是一样的,只是添加一个方向。

  RelativeLayout primary_layout = new RelativeLayout(this); LayoutParams layoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); primary_layout.setLayoutParams(layoutParam); // FOR LINEAR LAYOUT SET ORIENTATION // primary_layout.setOrientation(LinearLayout.HORIZONTAL); // FOR BACKGROUND COLOR primary_layout.setBackgroundColor(0xff99ccff); primary_layout.addView(createATextView(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_LEFT, list[i], 20, 10, 20)); primary_layout.addView(createATextView(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, RelativeLayout.ALIGN_PARENT_RIGHT, subTotal.toString(), 20, 10, 20)); 
 TextView rowTextView = (TextView)getLayoutInflater().inflate(R.layout.yourTextView, null); rowTextView.setText(text); layout.addView(rowTextView); 

这是我如何使用这个:

  private List<Tag> tags = new ArrayList<>(); if(tags.isEmpty()){ Gson gson = new Gson(); Type listType = new TypeToken<List<Tag>>() { }.getType(); tags = gson.fromJson(tour.getTagsJSONArray(), listType); } if (flowLayout != null) { if(!tags.isEmpty()) { Log.e(TAG, "setTags: "+ flowLayout.getChildCount() ); flowLayout.removeAllViews(); for (Tag tag : tags) { FlowLayout.LayoutParams lparams = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT); lparams.setMargins(PixelUtil.dpToPx(this, 0), PixelUtil.dpToPx(this, 5), PixelUtil.dpToPx(this, 10), PixelUtil.dpToPx(this, 5));// llp.setMargins(left, top, right, bottom); TextView rowTextView = (TextView) getLayoutInflater().inflate(R.layout.tag, null); rowTextView.setText(tag.getLabel()); rowTextView.setLayoutParams(lparams); flowLayout.addView(rowTextView); } } Log.e(TAG, "setTags: after "+ flowLayout.getChildCount() ); } 

这是我自定义的TextView命名标签:

 <?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="10dp" android:textAllCaps="true" fontPath="@string/font_light" android:background="@drawable/tag_shape" android:paddingLeft="11dp" android:paddingTop="6dp" android:paddingRight="11dp" android:paddingBottom="6dp"> 

这是我的tag_shape:

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#f2f2f2" /> <corners android:radius="15dp" /> </shape> 

效应:

在这里输入图像说明

在其他地方,我添加了与listview对话的语言名称的textviews:

在这里输入图像说明

在这里输入图像说明

布局

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

类文件

 setContentView(R.layout.layout_dynamic); layoutTest=(LinearLayout)findViewById(R.id.layoutTest); TextView textView = new TextView(getApplicationContext()); textView.setText("testDynamic textView"); layoutTest.addView(textView); 

如果您正在使用Linearlayout。 它的参数应该是“wrap_content”在您的布局xml中添加dynamic数据。 如果你使用匹配或填充父母,那么你不能看到输出。

它应该是这样的。

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>