在android中将ImageView居中到居中

在android中将ImageView居中到居中,android,android-layout,Android,Android Layout,我在线性布局中有一个ImageView,这个线性布局在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_pare

我在线性布局中有一个ImageView,这个线性布局在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徽标设置为线性布局的中心。 我附加了xml的一部分,其中包含ImageView。 我使用了ScrollView,如果应用程序用于小屏幕,那么用户可以滚动。 完整XML太大,因为它是按仪表板样式设计的。 期待您的回复。 谢谢。


将以下属性放置在LinearLayout中 android:layout\u gravity=“center”

上述属性将把子视图放置在LinearLayout的中心。

这个如何

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

可能是因为您的ImageButton与ImageView位于同一线性布局中。
因此,它们共享相同的空间,当您“居中”ImageView时,它将在自己的空间中居中。例如,如果您想将其水平“居中”,请添加属性
android:orientation=“vertical”
,然后尝试将其居中。 或者将ImageButton和ImageView放在单独的线性布局中。

试试这个

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

看到这个了吗


这可能会对您有所帮助

    <?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>
    <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>

将android:gravity=“center”放置在线性布局中。以使图像视图居中。请参阅以下代码

<?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>

将线性布局的属性重力放置到中心。 android:gravity=“center”
如果不起作用,请上传您的完整xml。

我希望这会对您有所帮助

    <?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>
    <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>

您应该在ScrollView中使用根布局,因为它是相对的

android:layout_centerInParent="true"
在视图的xml中,您希望位于RelativeLayout下的中心