【问题标题】:Trying to center a progress bar on an image [duplicate]试图在图像上居中进度条[重复]
【发布时间】:2015-09-09 20:18:37
【问题描述】:

我有一个已实现且看起来不错的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/splash_layout">
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/splashscreen"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"/>
</LinearLayout>

但是我需要在图像的中心添加一个进度条,所以我有这个起始代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

但是当我尝试将两者结合时,它会将图像推离屏幕。由于某种原因,我不能使用图形布局来渲染它,并试图看看我是否可以让它工作。

我的综合想法是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/splash_layout">
<RelativeLayout
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />
</RelativeLayout>
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/splashscreen"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"/>

</LinearLayout>

它似乎只是将图像推离屏幕或其他东西,我无法弄清楚发生了什么。

是否有办法通过将相对布局变为相对或绝对来将相对布局从 dom 中取出?然后图像会向上滑动,但相对布局仍将是 0,0,高度和宽度 == 父级。

【问题讨论】:

    标签: android xml


    【解决方案1】:
    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/splash_layout" >
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/splashscreen"
            android:scaleType="centerCrop"/>
    
        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"/>
    </RelativeLayout>
    

    关于布局的文档是here,“fill_parent”自第 8 版 SDK 起已弃用。请改用“match_parent”。

    【讨论】:

    • 我可以应用填充进度条将其推高一些吗?例如,我的图像在进度条下方有一些文字,看起来有点草率。我不确定是否可以利用 layout_padding 和 centerInParent
    • View 类中没有名为“layout_padding”的属性。有“layout_margin*”参数、“paddingLeft/paddingTop/etc”和“padding”。至于在 RelativeLayout 中使用它们 - 试试看会发生什么。
    【解决方案2】:

    试试这个:

        <RelativeLayout
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:orientation="horizontal">
    
                            <ImageView
                                android:id="@+id/imgCapaLivroCadastro"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_gravity="center_horizontal"
                                android:src="@drawable/myPicture" />
    
                            <ProgressBar
                                android:id="@+id/pbImgLivroCadEdit"
                                style="?android:attr/progressBarStyleSmall"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_centerInParent="true"
                                android:visibility="visible" />
    </RelativeLayout>
    

    【讨论】:

    • 你能不能也给它应用填充,这样它就不会通过给它一些填充而在父级(进度条)中居中?
    猜你喜欢
    • 2019-08-22
    • 2013-09-28
    • 1970-01-01
    • 2017-01-29
    • 2023-04-06
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多