2012-02-06 2 views
1

У меня есть ImageView внутри линейного макета, и этот LinearLayout находится внутри RelativeLayout, и этот относительный макет находится внутри ScrollView.Центрирование изображения в центре в android

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:background="@drawable/white" 
android:fillViewport="true" 
android:tileMode="repeat" > 

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent" 
     android:layout_height="30dip" 
     android:background="@drawable/black" 
     android:tileMode="repeat"> 
     <ImageButton 
      android:id="@+id/buttonMainBackClosetheApp" 
      android:src="@drawable/greenarrow" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/black" 
      android:tileMode="repeat"/> 
     <ImageView 
      android:src="@drawable/logo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     </LinearLayout> 

Я хочу установить логотип ImageView в центр LinearLayout. Я прикрепил часть xml, которая имеет ImageView. Я использовал ScrollView как таковой, если приложение используется на маленьких экранах, тогда пользователь может прокручивать. Полный XML слишком большой, поскольку он был разработан в стиле панели. Ждем вашего ответа. спасибо.

ответ

0
<ImageView 
     android:src="@drawable/logo" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:layout_height="wrap_content"/> 
+0

спасибо, но не работает. – Mukunda

0

Поместите следующий атрибут в LinearLayout андроида: layout_gravity = "центр"

выше атрибут разместит свои дочерние представления в центре в LinearLayout.

+0

спасибо, но не работает. – Mukunda

0

Как насчет этого?

<ImageView 
    android:src="@drawable/logo" 
    android:layout_width="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:layout_height="wrap_content"/> 
+0

спасибо, но не работает. – Mukunda

0

Возможно, это потому, что у вас есть ImageButton в пределах одного LinearLayout с ImageView.
Таким образом, они разделяют одно и то же пространство, и когда вы «центрируете» ImageView, он центрируется в своем собственном пространстве. Если вы хотите «центрировать» его по горизонтали, например, добавьте атрибут android:orientation="vertical", а затем попытайтесь центрировать его. Или поместите ImageButton и ImageView в отдельный LinearLayouts.

0
<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:tileMode="repeat" > 

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
    <LinearLayout 
     android:layout_alignParentTop="true" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:gravity="center" 
     android:tileMode="repeat"> 
     <ImageButton 
      android:id="@+id/buttonMainBackClosetheApp" 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@color/white" 
      android:tileMode="repeat"/> 
     <ImageView 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
     </LinearLayout> 
     </RelativeLayout> 
     </ScrollView> 
0

может быть, это может помочь вам.

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:tileMode="repeat" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center_horizontal" > 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" 
      android:tileMode="repeat" > 

      <TextView 
       android:id="@+id/text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="LINEAR LAYOUT" 
       android:textSize="20dip" /> 

      <ImageButton 
       android:id="@+id/buttonMainBackClosetheApp" 
       android:layout_width="50dip" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:background="#FFFFFF" 
       android:tileMode="repeat" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 

      <TextView 
       android:id="@+id/text1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="LINEAR LAYOUT" 
       android:textSize="20dip" /> 
     </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
0

Место андроида: гравитация = "центр" в LinearLayout. Чтобы ImageView приблизился к центру. Проверьте следующий код.

<?xml version="1.0" encoding="utf-8"?>  

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="match_parent" 
android:background="@drawable/white" 
android:fillViewport="true" 
android:tileMode="repeat" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >  

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="30dp" 
     android:layout_alignParentTop="true" 
     android:background="@drawable/black" 
     android:tileMode="repeat" 
     android:gravity="center" > 

     <ImageButton 
      android:id="@+id/buttonMainBackClosetheApp" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@drawable/black" 
      android:src="@drawable/greenarrow" 
      android:tileMode="repeat" /> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/images"/> 
    </LinearLayout> 
    </RelativeLayout> 
</ScrollView> 
0

Место атрибута гравитации для линейной компоновки к центру. android: gravity = "center" Если это не сработает, тогда выгрузите свой полный xml.

0

Я надеюсь, что это вам поможет.

<LinearLayout 
      android:layout_alignParentTop="true" 
      android:layout_width="fill_parent" 
      android:layout_height="30dip" 
      android:background="@drawable/black" 
      android:tileMode="repeat" 
      android:orientation="vertical" 
      android:layout_centerHorizontal="true"> 
      <ImageButton 
       android:id="@+id/buttonMainBackClosetheApp" 
       android:src="@drawable/greenarrow" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/black" 
       android:tileMode="repeat" 
       android:layout_gravity="center"/> 
      <ImageView 
       android:src="@drawable/logo" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center"/> 
</LinearLayout> 
0

Вы Шоуда использовать корневой раскладку внутри ScrollView является RelativeLayout и использовать

android:layout_centerInParent="true" 

в XML отображения вида вы хотите быть в центре под RelativeLayout

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