2016-08-16 45 views
0

我試圖以編程方式創建一個TextView,它將顯示在另一個不屬於同一活動但不顯示TextView的佈局中。以下是代碼:以編程方式創建的TextView未顯示

活動:

公共類LogActivity擴展AppCompatActivity {

LinearLayout textLayout; 
TextView textView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_one); 

    LayoutInflater layoutInflater=getLayoutInflater(); 

View textEntryLayout=layoutInflater.inflate(R.layout.layout_two, null); 

    textLayout=(LinearLayout) textEntryLayout.findViewById(R.id.layout); 
    textView=new TextView(this); 

    textView.setText("TextView"); 
    textView.setLayoutParams(new LayoutParams(
      LayoutParams.MATCH_PARENT, 
      LayoutParams.WRAP_CONTENT)); 
    textLayout.addView(textView); 

} 

佈局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/layout" 
      android:background="@color/editTextBG"> 

      <LinearLayout 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:gravity="center_vertical" 
       android:orientation="vertical" 
       android:padding="10dp"> 

       <TextView 
        android:id="@+id/name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/editTextFont" 
        android:textSize="35sp" /> 

       <TextView 
        android:id="@+id/log" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/editTextFont" 
        android:textSize="20sp" /> 

      </LinearLayout> 

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

       <TextView 
        android:id="@+id/date" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/editTextFont" 
        android:textSize="35sp" /> 

       <TextView 
        android:id="@+id/time" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginTop="10dp" 
        android:textColor="@color/editTextFont" 
        android:textSize="20sp" /> 
      </LinearLayout> 
    </LinearLayout> 

</ScrollView> 

回答

1

那是因爲你的容器layout,還有其他兒童的高度設定到match_parent。您將無法看到您添加了Java代碼的TextView,因爲以XML添加的其他子項填充了所有屏幕。

+0

其子元素特別隱藏的看法? –

+0

'layout'中的每個'LinearLayout',因爲它們的高度被設置爲'match_parent'。 –

0

您需要將textEntryLayout添加到您的佈局。您正在修改該充氣視圖,而不將其添加到(例如)layout_one中的佈局。

嘗試:

LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1); 
layout.addView(textEntryLayout); 
相關問題