2016-10-09 5 views
-3

私はログイン用のレイアウトを設計しています。しかし、私はレイアウトの重心を設定したい(フルレイアウトのコンテンツを画面の中央に設定)。私は重力センターを使用していますが、それは私に適切な出力を与えていないので、別の問題で問題が発生している可能性があります。ベローは私のレイアウトのXMLコードです。だから誰かが私に問題がある場所を教えてもらえますか?レイアウトの重心を設定するには

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="30dp" 
     android:paddingLeft="20dp" 
     android:layout_marginTop="10dp" 
     android:background="#458221" 
     > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_gravity="center" 
    <!-- I want to set this layout into the center of the screen ,This layout cann't set the gravity center --> 


     > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="User Name: " 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="user" 
      /> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="password" 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="pass" 
      /> 
    </LinearLayout> 

</LinearLayout> 
+1

たくさん仕組みを理解します。パフォーマンスの悪いアラート。そして、あなたはセンターではどういう意味ですか?縦、横、またはスクリーンで? –

+0

1つのレイアウトに2つのレイアウトを追加するだけです。だから、なぜそれはパフォーマンスの悪い警告です。あなたが私にそれに関する情報をくれば、私は恩恵を受けるでしょう。あなたに感謝したこと – opu

+0

線形レイアウト内に1つの子テキストビューがありますが、findviewbyidがすべてのネストでコストがかかるようなネストを避ける必要があります –

答えて

0

あなたのルート要素FrameLayoutまたはこのようなRelativeLayoutを行う必要があります。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="30dp" 
     android:paddingLeft="20dp" 
     android:layout_marginTop="10dp" 
     android:background="#458221" 
     > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_gravity="center" > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="User Name: " 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="user" 
      /> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="password" 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="pass" 
      /> 
    </LinearLayout> 

</FrameLayout> 
1

は、相対的なレイアウトを使用することを検討してください。次に、ユーザーをandroid:layout_centerInParent="true"に設定して、親を水平方向と垂直方向の中央に配置することができます。これに

0

変更重力:

android:gravity="center_vertical|center_horizontal" 

それはあなただけで以下のような最上位の親リニアレイアウトにandroid:gravity="center"を追加するには、問題

0

を解決する必要があります。

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="30dp" 
     android:paddingLeft="20dp" 
     android:layout_marginTop="10dp" 
     android:background="#458221" 
     > 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Hello" 
      /> 
    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:gravity="center"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="User Name: " 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="user" 
      /> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="password" 
      /> 
     <EditText 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="pass" 
      /> 
    </LinearLayout> 

</LinearLayout> 

アンドロイド:重力 - これらLayoutParamsに関連付けられたビューの 重力。

android:layout_gravity - 子供がその親に供給する標準的な重力定数。 X軸とY軸の両方で、その囲みレイアウト内での子ビューの配置方法を定義します。

0

アンドロイド:layout_gravity

android:layout_gravity

は、その括弧トン(レイアウト内の例えば子ビュー)内の要素の位置を設定するために使用されています。 LinearLayoutFrameLayout

アンドロイドによってサポート:重力

android:gravityは要素(内部のTextView例えばテキスト)内のコンテンツの位置を設定するために使用されます。

は取得するとの考えは、この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:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:orientation="vertical"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:layout_gravity="left" 
     android:gravity="center_vertical"> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/first" 
      android:background="@color/colorPrimary" 
      android:gravity="left"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/second" 
      android:background="@color/colorPrimary" 
      android:gravity="center"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/third" 
      android:background="@color/colorPrimary" 
      android:gravity="right"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:layout_gravity="center" 
     android:gravity="center_vertical"> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/first" 
      android:background="@color/colorAccent" 
      android:gravity="left"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/second" 
      android:background="@color/colorAccent" 
      android:gravity="center"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/third" 
      android:background="@color/colorAccent" 
      android:gravity="right"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:orientation="vertical" 
     android:layout_gravity="right" 
     android:gravity="center_vertical"> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/first" 
      android:background="@color/colorPrimaryDark" 
      android:gravity="left"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/second" 
      android:background="@color/colorPrimaryDark" 
      android:gravity="center"/> 

     <TextView 
      android:layout_width="@dimen/fixed" 
      android:layout_height="wrap_content" 
      android:text="@string/third" 
      android:background="@color/colorPrimaryDark" 
      android:gravity="right"/> 

    </LinearLayout> 

</LinearLayout> 
関連する問題