元素不能在Fragment上定位

时间:2017-02-25 22:49:48

标签: android android-fragments

我创建了一个Fragment元素,但是我无法将小部件定位在任何位置 - 它只是固定在左上角。

enter image description here

enter image description here

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context="layout.BlankFragment">

    <!-- TODO: Update blank fragment layout -->

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar5" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

    <ProgressBar
        style="?android:attr/progressBarStyle"
        android:layout_width="282dp"
        android:layout_height="259dp"
        android:id="@+id/progressBar4" />

</FrameLayout>

为什么会出现这个问题?这是我的第一个Android应用程序,但是当使用普通的Activity时,该位置可以正常工作。

1 个答案:

答案 0 :(得分:0)

设置android:layout_gravity。例如,如果您想将其居中:

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:id="@+id/progressBar5" />

您正在使用FrameLayout将儿童叠加在其他人之上。如果这不是您想要的,请考虑使用LinearLayoutRelativeLayout