2013-12-18 2 views
5

다음 구조의 abc.xml이 있습니다.Android : 중첩 된 레이아웃에 동적으로 뷰 추가

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<RelativeView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    </LinearLayout> 
</RelativeLayout> 
</ScrollView> 

선형 레이아웃에 동적으로 textviews를 추가하려고합니다. 아래는 제 코드입니다. 나는 어떤 오류도 내지 않지만 원하는 결과를 얻지 못하고있다. 당신의 XML은 텍스트 뷰를 추가

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
    <LinearLayout 
    android:id="@+id/linear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    > 
    </LinearLayout> 
</LinearLayout> 
</ScrollView> 

와 자바 코드해야

LayoutInflater Inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view = Inflater.inflate(R.layout.abc, null); 

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear); 

     TextView Tag = new TextView(getActivity()); 
     Tag.setText("textString"); 
     Tag.setBackgroundResource(R.color.bg_color); 
     Tag.setTextAppearance(getActivity(), R.style.SmallFont); 
     layout.addView(Tag); 
+0

왜 RelativeLayout을 사용하고 있습니까? –

+0

나는 텍스트 뷰와 버튼 뷰와 같은 상대 레이아웃의 다른 뷰도 가지고 있습니다. – Shah

답변

6

내가 아직 답을 발견 모르는

LinearLayout layout = (LinearLayout) view.findViewById(R.id.linear); 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
    LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
TextView Tag = new TextView(getActivity()); 
tag.setLayoutParams(params); 
Tag.setText("textString"); 
Tag.setBackgroundResource(R.color.bg_color); 
Tag.setTextAppearance(getActivity(), R.style.SmallFont); 
layout.addView(Tag); 
0

,하지만 난 그냥했다 이 문제를 우연히 발견하고 나는 1 가지 방법을 발견했다. 난 당신이 다음과 같이 하나의 비트의 레이아웃을 조정할 필요가 있다고 생각 :

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <RelativeView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <LinearLayout 
       android:id="@+id/linear" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" > 
      </LinearLayout> 
     </LinearLayout> 

    </RelativeView> 

</ScrollView> 
0

당신은 제공해야합니다, ID를 가지는 선형 레이아웃에

android:layout_orientation="either horizontal or vertical" 

: 선형.

관련 문제