Центрирование ImageView для центрирования в Android - PullRequest
1 голос
/ 06 февраля 2012

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

 <?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 слишком велик, так как он был разработан в стиле панели инструментов. Ждем Вашего ответа. спасибо.

Ответы [ 11 ]

0 голосов
/ 09 октября 2014

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

    <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 голосов
/ 28 сентября 2016

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

android:layout_centerInParent="true"

в xml представления вы хотите быть в центре под RelativeLayout

0 голосов
/ 28 августа 2014

Место android: gravity = "center" в 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 голосов
/ 28 августа 2014

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

0 голосов
/ 24 февраля 2012
<?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 голосов
/ 03 апреля 2012

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

    <?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 голосов
/ 06 февраля 2012

попробуйте это

 <ImageView
       android:id="@+id/logo"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"            
       android:layout_centerHorizontal="true"           
       android:background="@drawable/logo" />

посмотрите это

выравнивание по центру имени приложения в Android`

0 голосов
/ 06 февраля 2012

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

0 голосов
/ 06 февраля 2012

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

<ImageView 
    android:src="@drawable/logo"
    android:layout_width="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:layout_height="wrap_content"/>
0 голосов
/ 06 февраля 2012

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

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

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...