2017-03-11 152 views
0

我試圖以編程方式創建TextView,如左圖所示,但我運氣不佳。出於某種原因,「SKIP」和「>」被複製成兩行,如右圖所示。我附上我的相關代碼以供參考。請注意,左圖像是使用靜態xml佈局完成的。以編程方式創建TextView的原因是因爲我需要在第4張幻燈片中用一個按鈕替換TextView。請幫忙!以編程方式創建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); 
+3

的[創建新的TextView編程然後下面的另一TextView的顯示它(可能的複製http://stackoverflow.com/questions/4394293/create-a-new-textview-programmatically-then-display-it -below-another-textview) –

+0

感謝您的及時回覆。不,我只是在Activity的onCreate()方法中調用了我的Java代碼。 –

+0

我測試過你的代碼,SKIP「和」>「只顯示單行(如你所期望的),我認爲這個問題在其他地方,請顯示你的完整代碼 – tahsinRupam

回答

0

enter image description here

您好,這是我的榜樣。

獲取您的佈局,創建一個新的TextView並創建一個「可繪製」的圖像。

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