我有一个相对布局,在其中放置了 3 个项目、2 个 ImageView 和 1 个 ScrollView 。 我的布局是这样的...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="@drawable/background">
<RelativeLayout
android:id="@+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="@+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/button_background"
android:contentDescription="@string/hotelBtnDesc"
android:gravity="center"
android:src="@drawable/icon1" />
<TextView
android:id="@+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="@string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/down" />
</RelativeLayout>
</LinearLayout>
上面的代码生成如下所示的 View :
您可能会注意到箭头没有居中对齐,而是稍微向右移动。为什么会这样,我该如何解决它?请注意,我已经在我的 ImageView 中使用了 android:layout_centerHorizontal="true"。
提前谢谢
最佳答案
你的问题
android:layout_margin="8dp"
从 ScrollView 和 ImageView 中删除它
并将其直接传递给RelativeLayout
。
或添加
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
在您的RelativeLayout
中。
并通过
android:layout_centerHorizontal="true"
到你的 ScrollView
关于android - layout_centerHorizontal 未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311015/