2014-10-29 109 views
0

我很想爲Android設置一個XML格式的佈局,在不同的顯示器上顯示四行矩形圖像,每行兩行。 任何人都可以幫助我嗎? 我能得到的最好的是這樣的:Android XML佈局自動調整

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/logo" 
     android:layout_marginTop="12dp" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="16dp" /> 
    <ScrollView 
     android:layout_marginLeft="5sp" 
     android:layout_marginRight="5sp" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 
      <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:orientation="horizontal" 
      android:gravity="center" 
      android:layout_weight="0.45"> 
       <ImageView 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:src="@drawable/img1" 
        android:layout_weight="0.45" /> 
       <LinearLayout 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:orientation="horizontal" 
        android:gravity="center" 
        android:layout_weight="0.1"/> 
       <ImageView 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:src="@drawable/img2" 
        android:layout_weight="0.45" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dip" 
       android:orientation="vertical" 
       android:layout_weight="0.1" /> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="0dip" 
       android:orientation="horizontal" 
       android:gravity="center" 
       android:layout_weight="0.45"> 
       <ImageView 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:src="@drawable/img3" 
        android:layout_weight="0.45" /> 
       <LinearLayout 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:orientation="horizontal" 
        android:gravity="center" 
        android:layout_weight="0.1" /> 
       <ImageView 
        android:layout_width="0dip" 
        android:layout_height="match_parent" 
        android:src="@drawable/img4" 
        android:layout_weight="0.45" /> 
      </LinearLayout> 
     </LinearLayout> 
    </ScrollView> 
</LinearLayout> 

我想有類似Windows Phone的瓷磚的佈局...... 這是正確的使用屬性「layout_weight」爲了這個目的?

回答

1

要使用什麼是Gridview佈局:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:numColumns="2" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:gravity="center" 
/> 

,然後如果你添加四個項目連接到GridView適配器,你將最終獲得您想要的UI。

protected ArrayAdapter<Item> provideAdapterView() { 
     return new ArrayAdapter<Item>(getActivity(), R.layout.item){ 
      @Override 
      public View getView(int position, View view, ViewGroup parent) { 
       if(view==null) { 
        view = LayoutInflater.from(getActivity()).inflate(R.layout.item, parent, false); 
       //perform whatever you want to the layout that is inflated in the item file. 
       return view; 
      } 
     }; 
    } 

    @Override 
    public View performOnCreate(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_item_list, container, false); 
     adapter = provideAdapterView(); 
     GridView gridView = rootView.findViewById(R.id.gridview); 
     gridView.setAdapter(adapter); 
     return rootView; 
    }