2016-09-17 4 views
2

프로그래밍 방식으로 레이아웃 및 XML을 생성해야하는 경우가 필요합니다. 어떤 접근 방식을 취해야합니까? 저에게 알려주세요. 고맙습니다.Android : xml 병합 및 프로그래밍 방식으로 레이아웃 생성

생성 레이아웃 :

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 

    LinearLayout audio = new LinearLayout(this); 

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER); 

    RecordButton = new RecordButton(this); 
    audio.addView(RecordButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    PlayButton = new PlayButton(this); 
    audio.addView(PlayButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    setContentView(audio); 
} 

XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#FFFFFF" 
android:gravity="top"> 

<Button 
android:id="@+id/next" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_marginTop="130dp" 
android:background="#54C571" 
android:text="Next" 
android:textColor="#FFFFFF" 
android:textSize="23sp" 
android:textStyle="bold" /> 
+0

'some_view_in_xml.addView (generated_layout); –

+0

@KNeerajLal 선형 레이아웃은 어떻습니까? 어떻게 그들을 포함시켜야합니까? 이건'audio.xml.addView (generated)'와 같은가요? 나는 노력했고 XML은 해결할 수 없다고 말했다. –

+0

XML 레이아웃을 보여준다. –

답변

2

이는 XML이다,보기를 누른 것과 생성 된 뷰를 추가 할

을 컨테이너 뷰를 생성합니다
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#FFFFFF" 
    android:gravity="top"> 

    <Button 
     android:id="@+id/next" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="130dp" 
     android:background="#54C571" 
     android:text="Next" 
     android:textColor="#FFFFFF" 
     android:textSize="23sp" 
     android:textStyle="bold" /> 

    <!-- create a container view to hold your view --> 
    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/next" /> 

</RelativeLayout> 

코드에서

@Override 
public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.the_above_layout); 

    LinearLayout container = (LinearLayout) findViewById(R.id.container); 


    LinearLayout audio = new LinearLayout(this); 

    audio.setGravity(Gravity.BOTTOM | Gravity.CENTER); 

    RecordButton = new RecordButton(this); 
    audio.addView(RecordButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 

    PlayButton = new PlayButton(this); 
    audio.addView(PlayButton, 
       new LinearLayout.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT, 
        Gravity.CENTER | Gravity.BOTTOM)); 


    // add the view to your container 
    container.addView(audio); 
} 
+1

지금 받으십시오. 나는 당신 덕분에 오늘 새로운 무언가를 배웁니다. –

관련 문제