android - layout_centerHorizo​​ntal 未居中

标签 android android-layout

我有一个相对布局,在其中放置了 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 :

above code executed

您可能会注意到箭头没有居中对齐,而是稍微向右移动。为什么会这样,我该如何解决它?请注意,我已经在我的 ImageView 中使用了 android:layout_centerHorizo​​ntal="true"。

提前谢谢

最佳答案

你的问题

android:layout_margin="8dp"

从 ScrollView 和 ImageView 中删除它

并将其直接传递给RelativeLayout

或添加

android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"

在您的RelativeLayout中。

并通过

android:layout_centerHorizontal="true"

到你的 ScrollView

关于android - layout_centerHorizo​​ntal 未居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10311015/

相关文章:

android - 如何使用模拟器在 Galaxy S4 配置中测试 android 应用程序?

android - 以编程方式应用的 9-patch 挂起应用程序

android - CollapsingToolbarLayout 无法转换为 android.support.v7.widget.Toolbar

android - 从 Fragment 添加项目到操作栏的选项菜单后发生崩溃,然后更改方向

android - 以编程方式根据屏幕更改文本大小

android - 在 fragment 中使用FragmentManager?

android - 单个 XML、多个 Activity 以及性能

android - 需要有关 AutoCompleteTextView 搜索的帮助

android - GPS 和谷歌位置服务 API 与框架位置 API (android.location)

android - 使用 SimpleAdapter 过滤 listView 数据