简体   繁体   English

在Android中以编程方式添加无xml元素的视图

[英]Programatically adding views without xml element in android

Description :: 说明 ::

  • On click of button I want to add a textview dynamically on run-time 单击按钮时,我想在运行时动态添加textview
  • I don't want to use a textview widget(from xml) 我不想使用textview小部件(来自xml)
  • I want to generate textviews programatically 我想以编程方式生成textviews
  • Is it possible ? 可能吗 ?
  • If so how ? 如果可以,怎么办?

MainActivity.java 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 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>

Something like this: 像这样:

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

Just 只是

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

You can create a textView using the context of your activity. 您可以使用活动的上下文创建textView。

example: 例:

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

Simple example, including layout params (width, height, 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);

TextView in a LinearLayout: LinearLayout中的TextView:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM