2016-11-23 6 views
0

私は熟練したアンドロイドの開発者ではないので、私の知らないことを言い訳します。Android:十分な余裕を持ってLinearLayout内に複数のimageViewを中央に配置する

----------------------------------- 
|         | 
|   A   B   | 
|         | 
----------------------------------- 

IがのLinearLayoutを使用して中心合わせを使用して、私は取得しています午前:私は2画像を有するようになって水平に並べて見ています、それらの両方が、次の例のように十分な余裕がある適切ように中心これは:

----------------------------------- 
|         | 
|    AB    | 
|         | 
----------------------------------- 

私は動的その後、十分なマージンで再び自分自身を中央にすべきである複数の要素を追加する必要がありますので、パディングを避けたいです。 linearLayoutで行うことができますか、そうでない場合は別のレイアウトを提案することをお勧めします。

答えて

1

LinearLayoutでは2つの部分に分割されているため、の重量はです。

 <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
android:oriantion="horizontal" 
     android:weightSum="2"> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="1" /> 

     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="3" /> 

     </LinearLayout> 
+0

申し訳ありませんが試してみてください。.. –

+0

になります実際には、まったくweightSumを追加する必要がありますか?要素を追加すると、weightSumを変更する必要があります。私はそれをすべて指定しなければ、自動的に正しく計算されると思いますか? –

+0

はい、もう1つのイメージを追加して、linearlayoutでweightsum = 3とimageview weight = 1を指定した場合 –

1

次のコードを使用してください。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:gravity="center" 
    > 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/ic_launcher"/> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="50dp" 
     android:src="@mipmap/ic_launcher"/> 

</LinearLayout> 

だけSRCであなたのイメージを置き換えます。これはあなたのために働くことを望みます!

+0

実際には、ただ2つの要素のハードコーディングを避けたいと思います。私はプログラムでimageViewをさらに追加し、レイアウトを独自に調整することを好みます。とにかくありがとう。 –

+0

@EkanshGuptaに画像の数に制限はありますか? –

+0

はい、5と言います。 –

1

これは、layout_weightが動作するようになると、これはアイテムを線形レイアウトに配置し、各ビューがどのくらいのスペースを取るかを示す方法です。したがって、1つの線形レイアウトの場合、すべての子供の合計重量が、最初のイメージのレイアウトの重みが1で、もう1つが1である場合、各ビューがどれくらいの割合で取るかを指定します。次に、このレイアウトの場合、合計は2になります。 1/2)半分であり、他方は半分である。あなたはまた、xmlのようなプログラムでレイアウトの重みを変更することができます!

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal"> 

<ImageView 
    android:id="@+id/image1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 

<ImageView 
    android:id="@+id/image2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    /> 
</LinearLayout> 

リニアレイアウトにはオプションの属性weightsumがありますが、システムはそれをとにかく計算します。この例では2

2

私の悪い編集この

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:weightSum="1" 
    android:orientation="horizontal"> 

    <ImageView 
     android:id="@+id/image1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/ic_launcher" 
     android:layout_weight=".5" /> 

    <ImageView 
     android:id="@+id/image2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:src="@mipmap/ic_launcher" 
     android:layout_weight=".5" /> 
</LinearLayout> 
関連する問題