2016-04-05 34 views
0

まあ、私は多くのサイトと質問を参照しましたが、私はそれらを試しても、私は必要なものを適切な位置合わせを得ることができません。私は線形レイアウトで水平方向に中心を持つテキストビューが必要です。以下は水平線形レイアウトでテキストビューを中央に配置するにはどうすればよいですか?

は私のコードです:

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/fifteen_dp" 
     android:paddingRight="@dimen/fifteen_dp" 
     android:paddingTop="@dimen/fifteen_dp" 
     android:paddingBottom="@dimen/fifteen_dp" 
     android:orientation="horizontal"> 

     <ImageView 
      android:layout_width="30dp" 
      android:layout_height="30dp" 
      android:layout_gravity="left" 
      android:background="@drawable/menu_icon"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/subscribe" 
      android:layout_gravity="center_horizontal" 
      android:textSize="@dimen/header" /> 

答えて

0

あなたはImageViewの上のTextViewをオーバーラップしようとしているなら、あなたはRelativeLayoutかでframeLayoutでそれを試してみてください。 LinearLayoutは、ビューの向きに応じてビューを配置します。 あなたのデザインに応じてweightsumlayout_weightを試すことができます。ここで

は、私はあなたのために作成したサンプルです:

コード:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="100dp" 
android:orientation="horizontal" 
android:weightSum="1"> 

<ImageView 
    android:background="@android:color/black" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight=".3" /> 

<TextView 
    android:gravity="center" 
    android:layout_width="0dp" 
    android:text="Center Text" 
    android:textSize="30sp" 
    android:layout_height="match_parent" 
    android:layout_weight=".4" /> 

<ImageView 
    android:background="@android:color/black" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight=".3" /> 

関連する問題