2016-12-06 23 views
0

私は、内部に3つのアイテムを持つ水平線形レイアウトを持っています。ビューを横方向のリニアレイアウトの中央に水平に配置する方法

<?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="wrap_content" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:id="@+id/FirstItem" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_microphone" /> 

    <EditText 
     android:id="@+id/secondItem" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/ThirdItem" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0"/> 

</LinearLayout> 

コードでは、2番目と3番目の項目を非表示にすることがあります。他の2人が隠れているときに、私の最初のアイテムが真ん中に表示されることを願っています。

どうすればよいですか?現在、2番目と3番目のアイテムを非表示にすると、最初のアイテムはまだ左側に残ります。最初の項目でlayout_weight = 1に設定できましたが、2と3が表示されているときは正しく表示されません。

答えて

1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:orientation="horizontal"> 

    <ImageButton 
     android:id="@+id/FirstItem" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:src="@drawable/ic_microphone" /> 

    <EditText 
     android:id="@+id/secondItem" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" /> 

    <ImageButton 
     android:id="@+id/ThirdItem" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_weight="0"/> 

</LinearLayout> 
1

xmlにandroid:gravityまたはandroid:layout_gravityを使用してください。

android:gravityは、コンテンツの重力を設定します。

android:layout_gravityは、その親にレイアウトの重力を設定します。

編集

使用android:gravity="center"あなたのコードでそれが動作します。

+0

私はカップルを試しました。うまくいかないようです。実際に幅全体を塗りつぶすことなく水平線形レイアウトで項目を水平に配置することは可能ですか? – user1017674

+0

その両方を試してみると、いつ使うか、いつ使うかが分かります。ここでは、問題を解決するために人々を案内していますが、実際に問題を解決するわけではありません。あなたの解決策がまだ見つからない場合は、私たちが手伝ってくれるでしょう:) –

+0

実際には、横幅をいっぱいにしないで水平線形レイアウトで項目を水平に配置することはできますか? – user1017674

関連する問題