2016-08-16 4 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); 

} 

레이아웃 :

컨테이너 layout은, 그 높이 설정되어 다른 아이들이 있기 때문에입니다
<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

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); 
관련 문제