2016-08-16 19 views
0

TextViewをプログラムで作成しようとしていますが、これは同じアクティビティに属していない別のレイアウトで表示されますが、TextViewは表示されません。以下のコードです: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は、その高さに設定されている他の子供を持っているためです。

パブリッククラス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); 

} 

レイアウトを拡張しますmatch_parent。 XMLで追加された他の子がすべての画面を埋めるので、Javaコードで追加するTextViewを見ることはできません。

+0

子要素は、具体的ビューを隠していますか? –

+0

それぞれの 'LinearLayout'は' layout'内にあります。その高さは 'match_parent'に設定されています。 –

0

レイアウトにtextEntryLayoutを追加する必要があります。あなたはあなたのlayout_oneのレイアウトにそれを追加することなくその膨張したビューを修正しています。

試してみてください。

LinearLayout layout = (LinearLayout)findViewById(R.id.layout_id_in_layout1); 
layout.addView(textEntryLayout); 
関連する問題