2016-08-16 48 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); 
相关问题