2015-02-27 3 views
0

Это мой макет, который является бар действие, которое я построил:Почему этот центр не является центром?

<LinearLayout 
     android:orientation="horizontal" 
     android:id="@+id/Layout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_blue_no_border"> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:orientation="horizontal"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/white" 
       /> 
      <TextView 
       android:id="@+id/textActionBar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="@color/white" 
       android:text="@string/app_name" 
       android:textStyle="bold"/> 
      </LinearLayout> 

    </LinearLayout> 

Unfortunely изображение находится на левой стороне и TextView рядом с изображением. Я хочу, чтобы вторая линейная макет была сосредоточена в первом макете. Что мне делать?

+0

использовать гравитацию insted of layout_centerHorizontal – Muthu

+0

Вы хотите центрировать текст и оставить изображение слева? Или сосредоточить их обоих? –

ответ

0
<RelativeLayout 
     android:orientation="horizontal" 
     android:id="@+id/Layout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/button_blue_no_border"> 
     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:orientation="horizontal"> 
      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/white" 
       /> 
      <TextView 
       android:id="@+id/textActionBar" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textColor="@color/white" 
       android:text="@string/app_name" 
       android:textStyle="bold"/> 
      </LinearLayout> 

</RelativeLayout> 

Или посмотрите здесь How to align LinearLayout at the center of its parent?

0

android:layout_centerHorizontal="true" работает только для RelativeLayouts.

Вы можете попробовать сделать родительский макет как RelativeLayout.

Или добавьте layout_gravity="center_horizontal" в качестве атрибута для детского макета.

Или добавьте gravity="center_horizontal" как атрибут родительского макета.

0

android:layout_centerHorizontal работает только в пределах RelativeLayout.
В LinearLayout вы должны использовать gravity

0

Просто добавьте

android:gravity="center_horizontal" 

для вашего родительского компоновщика и вы золотой ... или алмаз. без разницы.

0

Назначьте android:layout_gravity="center_horizontal" в макете вашего ребенка, а также присвойте imageView и textView android:layout_weight="1" свойство для разделения равных частей пространства в вашем макете ребенка.

Попробуйте этот код.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/Layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" > 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:layout_margin="10dp" 
       android:orientation="horizontal" > 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/ic_launcher" /> 

       <TextView 
        android:id="@+id/textActionBar" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_weight="1" 
        android:text="Hello" 
        android:textStyle="bold" /> 
      </LinearLayout> 


     </LinearLayout> 

В TextView эта линия android:layout_gravity="center_vertical" определяет, что TextView также центр с высоты изображения. Так что это до вас, в вашем приложении это нужно или нет.

Смежные вопросы