2011-12-19 3 views
1

프로그래밍 방식으로 Android에서 splitView를 만들려고합니다. 이 기능은 완전히 새로운 것입니다. 몇 가지 조사를 한 후에, 나는 조각이 사용되어야한다는 것을 깨달았다.은 splitView를 프로그래밍 방식으로 구현합니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <fragment android:name="com.example.news.ArticleListFragment" 
      android:id="@+id/list" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
    <fragment android:name="com.example.news.ArticleReaderFragment" 
      android:id="@+id/viewer" 
      android:layout_weight="2" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" /> 
</LinearLayout> 

LinearLayout이 레이아웃에 가장 적합한 옵션입니까? 두 번째로 단추, 피커 같은 UI 파트를 프로그램 적으로 추가 할 수 있습니까?

어떤 도움을 주셔서 감사합니다. 예가 더 좋을 수도 있습니다. 감사.

답변

0

Fragments가있는 레이아웃을 사용할 수 있습니다. 요구 사항에 완전히 달려 있습니다.

예, 단추 등의보기를 조각에 프로그래밍 방식으로 추가 할 수 있습니다. Fragment의 레이아웃을 Layout으로 선언하십시오. 자식이

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
    ViewGroup mViewGroup = inflater.inflate(R.layout.main, container, false); 
    Button mButton = new Button(getActivity()); 
    mButton.setLayoutParameters(new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.FILL_PARENT, 
           LinearLayout.LayoutParams.FILL_PARENT)); 
    mViewGroup.add(mButton); 
} 
+0

덕분에 Rajdeep, 당신은 예를 제공 할 수주십시오? 감사. – user788511

+0

예제를 표시 할 응답을 수정했습니다 –

+0

Rajdeep 감사합니다. 그러나 Fragment에보기를 추가하는 것이 좋습니다. 귀하의 예제는 레이아웃에보기를 추가하는 것 같습니다. 제발 조언. – user788511

1

를 볼되지 당신이하는 낮은 안드로이드 버전에서 조각 레이아웃 또는 건물 응용 프로그램을 사용하려는 해달라고하면

프로그램 뷰를 추가 한 예는 --- R.layout.main가있는 LinearLayout 수 조각 지원 기능이없는 경우이 경우 하나의 작업에서 두 ​​개의 별도 XML 파일을 호출하여 분할보기를 얻을 수 있습니다. 그냥하지만 ​​왼쪽과 오른쪽에 요구 사항에 따라 두 개의 XML을 빌드합니다. 작은 예를

LinearLayout layoutMain = new LinearLayout(this); layoutMain.setOrientation(LinearLayout.HORIZONTAL); setContentView(layoutMain); LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate( R.layout.firstxml, null); RelativeLayout layoutRight = (RelativeLayout) inflate.inflate( R.layout.secondxml, null);

RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.WRAP_CONTENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 
layoutMain.addView(layoutLeft, 100, 100); 
layoutMain.addView(layoutRight, relParam); 

} 의견을 추가 관하여 `

관련 문제