【问题标题】:Center Horizontal doesn't work水平居中不起作用
【发布时间】:2014-10-16 07:53:25
【问题描述】:

我正在尝试将 imagebutton 的位置设置在中心,但它不起作用。

现在,当我尝试将其设置为居中时,它几乎向右移动。

谁能告诉我我的代码哪里出错了?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#EBEBEB" >
    <LinearLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="center_horizontal"
    android:gravity="center_horizontal"
    android:orientation="vertical">

        <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="#EBEBEB" >
        <ImageView
        android:id="@+id/imagefield"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginTop="5dp"
        android:src="@drawable/imagefield"
        android:background="@null"/>
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:onClick="imagebutton1"
        android:layout_alignBottom="@+id/imagefield"
        android:layout_centerHorizontal="true"
        />   
        <ProgressBar
         android:id="@+id/progressBar1"
         style="?android:attr/progressBarStyleHorizontal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_above="@+id/imagebutton1"/> 



</RelativeLayout>
</LinearLayout>
</ScrollView>

【问题讨论】:

  • 可能是因为你的 layout_alignBottom 属性设置在 imageView..
  • @Opiatefuchs 不,不是因为它。
  • 您能描述一下您希望 ImageView 相对于 ImageButton 的位置吗?
  • 我希望 ImageButton 在底部,ImageView 的中心

标签: android layout center android-xml


【解决方案1】:

复制粘贴到下面的代码

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#EBEBEB" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:background="#EBEBEB" >

        <ImageView
            android:id="@+id/imagefield"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="5dp"
            android:background="@null"
            android:src="@drawable/ic_action_chat" />

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:onClick="imagebutton1" />

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/imagefield"
            android:layout_alignTop="@+id/imagefield"
             />

    </RelativeLayout>

</ScrollView>

【讨论】:

  • 只使用相对布局
【解决方案2】:

ImageButton中添加android:gravity="center"属性

<ImageButton
        ...
        android:gravity="center"
        ...
        />   

【讨论】:

  • 没有用,并且这个代码出现在左侧,顶部图像视图。
【解决方案3】:

如果您希望“...ImageButton 在底部,ImageView 的中心”

然后您需要将它们都放在 FrameLayout 中。

  <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:background="#EBEBEB" >
        <ImageView
            android:id="@+id/imagefield"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/imagefield"
            android:background="@null"/>
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:onClick="imagebutton1"
            android:layout_alignBottom="@+id/imagefield"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal|bottom" />
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/imageButton1"
            android:layout_centerHorizontal="true" />
    </FrameLayout>

【讨论】:

    【解决方案4】:

    试试这个:

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/imagefield"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_marginTop="5dp"
            android:src="@drawable/ic_menu_home"
            android:layout_centerHorizontal="true"
            />
    
        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:drawable/ic_menu_share"
            android:layout_centerHorizontal="true"
            android:layout_alignBottom="@id/imagefield"
            />
    
    </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 2011-07-06
      • 2014-12-11
      • 1970-01-01
      相关资源
      最近更新 更多