2015-05-01 34 views
0

我有一個佈局,其中一個元素動態地改變其大小(紅色imageView)。問題是,下面的視圖或者不存在或者不對中。適應佈局以改變圖像的寬度查看

前兩個圖像顯示它看起來應該像:

LinearLayout低於設定到match_parent big imageView, match_parent http://i58.tinypic.com/35jedxt.png

LinearLayout低於設定到wrap_content small imageView, wrap_content http://i58.tinypic.com/255qjxs.png

這就是它的樣子在LinearLayoutlayout:width屬性是錯誤的: big imageView, wrap_content http://i61.tinypic.com/x1zk1h.png small imageView, match_parent http://i57.tinypic.com/2eutqmq.png

這裏是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" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true"> 

     <ImageView 
      android:layout_width="200dp" 
      android:layout_height="200dp" 
      android:id="@+id/imageView" 
      android:layout_gravity="center_horizontal" 
      android:background="#ffff0000" /> 

     <LinearLayout 
      android:orientation="horizontal" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:layout_gravity="center_horizontal"> 

      <Space 
       android:layout_width="20px" 
       android:layout_height="20px" 
       android:layout_weight="1" /> 

      <ImageView 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:id="@+id/imageView2" 
       android:background="#ff0000ff" /> 

      <Space 
       android:layout_width="20px" 
       android:layout_height="20px" 
       android:layout_weight="1" /> 

      <ImageView 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:id="@+id/imageView3" 
       android:background="#ff00ff00" /> 

      <Space 
       android:layout_width="20px" 
       android:layout_height="20px" 
       android:layout_weight="1" /> 
     </LinearLayout> 
    </LinearLayout> 

</RelativeLayout> 

怎麼可能適應的佈局,所以綠色和藍色元素始終圍繞和空間當紅色imageView很大時擴大,但當它太小時不切斷?

我更喜歡xml解決方案,因爲這只是一個簡單的例子,以編程方式執行此操作可能會變得複雜。

回答

1

我原來的答覆錯過要求「當紅色ImageView的大,空間擴展」。

你可以使用GridLayout來做到這一點 - layout_columnWeight僅適用於21,或者其他的東西。所以,你可能會想使用v7.gridlayout支持庫,但它會是這個樣子:

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<GridLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="400dp" 
     android:layout_height="200dp" 
     android:layout_columnSpan="5" 
     android:layout_gravity="center" 
     android:layout_row="0" 
     android:background="#ffff0000" 
     /> 

    <Space 
     android:layout_width="20px" 
     android:layout_height="20px" 
     android:layout_column="0" 
     android:layout_row="1" 
     android:layout_columnWeight="1" 
     /> 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_column="1" 
     android:layout_row="1" 
     android:background="#ff0000ff" /> 

    <Space 
     android:layout_width="20px" 
     android:layout_height="20px" 
     android:layout_column="2" 
     android:layout_columnWeight="1" 
     android:layout_row="1" /> 

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_column="3" 
     android:layout_row="1" 
     android:background="#ff00ff0a" /> 

    <Space 
     android:layout_width="20px" 
     android:layout_height="20px" 
     android:layout_column="4" 
     android:layout_columnWeight="1" 
     android:layout_row="1" /> 

</GridLayout> 
</FrameLayout> 
+0

謝謝你,它與'android.support.v7.widget.GridLayout'工作! – agrajag

0

是的,你可以玩的LinearLayout和重量,像

<LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center_horizontal" 
     android:layout_weight="0dp" 
     android:orientation="horizontal"> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:padding="10dp"> 

      <ImageView 
       android:id="@+id/imageView2" 
       android:layout_gravity="center" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="#ff0000ff" /> 

     </FrameLayout> 

     <FrameLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:padding="10dp"> 

      <ImageView 
       android:layout_gravity="center" 
       android:id="@+id/imageView3" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:background="#ff00ff00" /> 

     </FrameLayout> 

    </LinearLayout> 
相關問題