Android 动态创建 Textview

在这里插入图片描述

    <LinearLayout
        android:id="@+id/layout_handy_house_detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center"
        android:layout_margin="20dp">

    </LinearLayout>
		for(int i = 0; i < 4; i++) {
    
    
            final TextView textView = new TextView(this);
            Drawable drawable = getResources().getDrawable(R.drawable.house_detail_shape);

			textView.setText("Text "+i);
            textView.setTextColor(0xff2588D4);
            textView.setBackground(drawable);
            textView.setPadding(30,8,30,8);

            int curTextViewId = prevTextViewId + 1;

            textView.setId(curTextViewId);

            final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            //设置权重
            //params.weight=1.0f;
            //设置间距
            params.setMargins(0,0,20,0);
            //设置布局参数
            textView.setLayoutParams(params);
            prevTextViewId = curTextViewId;
            layout.addView(textView, params);
        }

猜你喜欢

转载自blog.csdn.net/qq_27494201/article/details/128675801
今日推荐