2016-08-03 46 views
0

経由ここでは私のXMLコードで、それはのLinearLayoutのID =「linearLayout2」の前に立ってImageViewのを持っていますアンドロイド - のLinearLayoutでスワップビューXML

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

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1" 
     android:background="@drawable/border" 
     android:paddingBottom="5dp"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/imageView" 
      android:src="@drawable/ic_swap" 
      android:layout_gravity="center_vertical" /> 

     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/linearLayout2" 
      android:paddingBottom="5dp" 
      android:layout_marginRight="5dp"> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/fromStationField" 
       android:completionThreshold="1" 
       android:hint="From" 
       android:textAlignment="textStart" 
       android:gravity="left|start" 
       android:layout_gravity="left" /> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/toStationField" 
       android:completionThreshold="1" 
       android:hint="To" 
       android:background="@android:color/transparent" 
       android:paddingRight="4dp" 
       android:paddingLeft="4dp" /> 

     </LinearLayout> 
    </LinearLayout> 
</RelativeLayout> 

私が何をしたいのかImageViewのは、後に立っているようにすることですLinearLayout id = "linearLayout2"私は画像の下に示すように。

xmlのみを使用してこれを行う方法は?以下のレイアウトで

enter image description here

+0

だから、左からではなく右側から画像ビューが必要ですか? – sJy

+0

sJy、はい、正確です。 – gigs

答えて

0

我々はlayout_weightプロパティを使用する必要はありませんし、match_parentを使用し続けることができるように、私はImageViewにおける負のマージンとImageViewの左LinearLayoutコンテナ内の正のマージンを使用しています。このためにはImageViewのサイズを修正する必要があります。このコードをチェックアウト:

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

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/linearLayout1" 
     android:background="@drawable/border" 
     android:paddingBottom="5dp"> 



     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/linearLayout2" 
      android:layout_marginRight="55dp" 
      android:paddingBottom="5dp"> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/fromStationField" 
       android:completionThreshold="1" 
       android:hint="From" 
       android:textAlignment="textStart" 
       android:gravity="left|start" 
       android:layout_gravity="left" /> 

      <AutoCompleteTextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/toStationField" 
       android:completionThreshold="1" 
       android:hint="To" 
       android:background="@android:color/transparent" 
       android:paddingRight="4dp" 
       android:paddingLeft="4dp" /> 

     </LinearLayout> 

     <ImageView 
      android:layout_width="50dp" 
      android:layout_height="50dp" 
      android:id="@+id/imageView" 
      android:src="@drawable/ic_swap" 
      android:layout_marginLeft="-55dp" 
      android:layout_gravity="center_vertical" /> 

    </LinearLayout> 
</RelativeLayout> 
+0

それはうまくいくので、あなたはアンドロイドのイメージのおよその幅を使っていますか?layout_marginLeft = " - 55dp"? – gigs

+0

@gig実際、ImageViewの高さと幅をそれぞれ50dpに固定しました。次に、5dpのマージンを追加し(美学的理由から)、マイナスマージンとして設定しました。 'ImageView'の幅を60dpに変更する場合は、余白などに-65dpを使います。 – Shaishav

+0

ありがとうございます。私はあなたの答えを受け入れる。 – gigs

1

一つの解決策は、linearLayout2後ImageViewのを移動するためにlinearLayout1の内側にある可能性があります。次に、linearLayout1の幅をmatch_parentに、兄弟ビューの幅を0dpに設定します。最後に、layout_weightを使用して、ImageViewおよびLinearLayout2に必要なサイズの割合を達成します。

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

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/border" 
     android:orientation="horizontal" 
     android:paddingBottom="5dp" 
     android:weightSum="10"> 

     <LinearLayout 
      android:id="@+id/linearLayout2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_marginRight="5dp" 
      android:layout_weight="9" 
      android:orientation="vertical" 
      android:paddingBottom="5dp"> 

      <AutoCompleteTextView 
       android:id="@+id/fromStationField" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="left" 
       android:completionThreshold="1" 
       android:gravity="left|start" 
       android:hint="From" 
       android:textAlignment="textStart" /> 

      <AutoCompleteTextView 
       android:id="@+id/toStationField" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/transparent" 
       android:completionThreshold="1" 
       android:hint="To" 
       android:paddingLeft="4dp" 
       android:paddingRight="4dp" /> 

     </LinearLayout> 

     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_weight="1" 
      android:src="@android:drawable/ic_swap" /> 

    </LinearLayout> 
</RelativeLayout> 
関連する問題