以编程方式创建TextView

时间:2017-03-11 17:17:38

标签: android textview

我正在尝试以编程方式创建一个TextView,如左侧图片所示,但我运气不好。出于某种原因," SKIP"和">"如右图所示,在两行中重复。我附上下面的相关代码供参考。请注意,左图像是使用静态xml布局完成的。以编程方式创建TextView的原因是因为我需要在第4张幻灯片中用按钮替换TextView。请帮忙!

相对xml布局部分

<RelativeLayout
    android:id="@+id/view_pager_indicator"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="5dp"
    android:layout_marginBottom="24dp"
    android:gravity="center">

    <LinearLayout
        android:orientation="vertical"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/pager_info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center"
            android:layout_marginBottom="20dp">

<!--<TextView--> (Want to replace this part with programmatic creation)
                <!--android:layout_width="wrap_content"-->
                <!--android:layout_height="wrap_content"-->
                <!--android:gravity="center"-->
                <!--android:textSize="20dp"-->
                <!--android:text="Welcome to Central Park!" />-->

        </LinearLayout>

        <RelativeLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/btn_skip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dip"
                android:gravity="left"
                android:text="SKIP"
                android:textSize="20dp" />

            <LinearLayout
                android:id="@+id/view_pager_countDots"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal" />

            <TextView
                android:id="@+id/btn_next"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="15dip"
                android:gravity="right"
                android:text=">"
                android:textSize="30dp" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

Java代码

pager_info = (LinearLayout) view.findViewById(R.id.pager_info);
TextView msg = new TextView(MyActivity.this);
msg.setText(message[0]);
msg.setTextSize(16);
msg.setLayoutParams(new LinearLayout.LayoutParams(
    new LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT)));
msg.setGravity(Gravity.CENTER);
pager_info.addView(msg);

1 个答案:

答案 0 :(得分:0)

enter image description here

您好,这是我的例子。

获取您的布局,创建一个新的TextView并创建一个&#34; Drawable&#34;与你的形象。

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

    TextView textView = new TextView(this);
    textView.setText("XPTO");
    textView.setPadding(10,10,10,10);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 15, 5, 0);
    params.alignWithParent = true;
    textView.setLayoutParams(params);
    textView.setGravity(Gravity.CENTER);

    Drawable img = getBaseContext().getResources().getDrawable(R.mipmap.ic_check_circle_black_24dp);
    img.setBounds( 0, 0, 60, 60 );
    textView.setCompoundDrawables( img, null, null, null );
    relativeLayout.addView(textView);