【问题标题】:ProgressBar not showing in layoutProgressBar 未在布局中显示
【发布时间】:2018-04-03 21:35:08
【问题描述】:

我正在尝试实现一个旋转的progressBar,但无论我尝试什么都不会显示。这是xml:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
tools:context="com.example.home.quoteapp.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@color/background">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:background="@drawable/rectangle">

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/search"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentEnd="true"
                android:layout_marginEnd="10dp"
                android:layout_marginTop="18dp"
                android:background="@drawable/ic_search_black_24dp"
                android:clickable="true"
                android:focusable="true"
                android:gravity="center" />

            <Button
                android:id="@+id/back_arrow_search"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_marginTop="18dp"
                android:background="@drawable/ic_arrow_back_black_24dp"
                android:clickable="true"
                android:focusable="true"
                android:visibility="invisible" />

            <Button
                android:id="@+id/nav_button"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_marginTop="18dp"
                android:background="@drawable/ic_more_vert_black_24dp"
                android:clickable="true"
                android:focusable="true" />

            <EditText
                android:id="@+id/section"
                android:layout_width="230dp"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_marginLeft="30dp"
                android:layout_toEndOf="@+id/nav_button"
                android:layout_toRightOf="@+id/nav_button"
                android:background="@android:color/transparent"
                android:clickable="false"
                android:ems="10"
                android:focusable="false"
                android:gravity="left"
                android:inputType="textMultiLine|textNoSuggestions"
                android:maxHeight="140dp"
                android:scrollbars="vertical"
                android:text="@string/section"
                android:textColor="@color/textColor"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/searchText"
                android:layout_width="230dp"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/section"
                android:layout_alignBottom="@+id/section"
                android:layout_alignStart="@+id/section"
                android:background="@drawable/underline"
                android:ems="10"
                android:hint="Search"
                android:imeOptions="actionSearch"
                android:inputType="textMultiLine|textNoSuggestions|text"
                android:maxHeight="140dp"
                android:textColor="@color/textColor"
                android:textColorHint="@color/textHint"
                android:textCursorDrawable="@null"
                android:textSize="20sp"
                android:visibility="invisible" />
        </RelativeLayout>
    </android.support.v7.widget.Toolbar>

    <RelativeLayout
        android:id="@+id/progressBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:visibility="visible" />
    </RelativeLayout>

    <GridView
        android:id="@+id/gridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginEnd="5dp"
        android:layout_marginStart="5dp"
        android:layout_marginTop="5dp"
        android:alwaysDrawnWithCache="true"
        android:clipChildren="true"
        android:columnWidth="90dp"
        android:gravity="center"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:padding="0dp"
        android:scrollingCache="true"
        android:smoothScrollbar="true"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp"></GridView>

</android.support.design.widget.AppBarLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navView"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/headerColour"
    android:theme="@style/NavigationTheme"
    android:visibility="invisible"
    app:itemBackground="@drawable/nav_item_background"
    app:itemTextColor="@color/headerColourAccent"
    app:menu="@menu/menu">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:background="@color/headerColourAccent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="left"
            android:gravity="left"
            android:paddingLeft="15dp"
            android:paddingTop="15dp"
            android:text="Sections"
            android:textColor="@color/headerColour"
            android:textSize="30dp" />

    </LinearLayout>
</android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

这就是我在MainActivity 中为progressBar 调用的内容。

private RelativeLayout progressBarLayout;

private ProgressBar progressBar


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    progressBarLayout = findViewById(R.id.progressBarLayout);
    progressBar = findViewById(R.id.progressBar);
    progressBar.getIndeterminateDrawable().setColorFilter(0xF09D00, PorterDuff.Mode.MULTIPLY);
}

然后我会通过这样做让progressBar消失

progressBarLayout.setVisibility(View.GONE);

progressBar 从未显示,当我将可见性设置为继续progressBarLayout 时,它确实消失了。

【问题讨论】:

  • 尝试为progressBar设置准确的宽度/高度值(不是wrap_content,如您的代码所示)
  • 即使我将宽度和高度设置为 100dp 或 match_parent,它也不显示
  • 这个进度条出现在 appbar 布局中是否有特定原因?如果不只是将进度条和网格视图放在 之后,您将看到它们
  • 我试过了,但它也没有出现。 hastebin.com/ozasizajih.xml

标签: java android user-interface progress-bar


【解决方案1】:

问题在于您的布局层次结构。我做了一些改动。 根现在是包含DrawerLayoutRelativeLayout,并且Progressbar 被删除到DrawerLayout 之外,现在位于DrawerLayout 下方的根布局内。此外,AppBarLayout 占用了整个屏幕的宽度和高度,我猜这不是您想要的,因此更改为,因为它阻止了它下面的其他视图

这是一个工作布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--Drawer related content below here-->

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="top">

                <Button
                    android:id="@+id/search"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_alignParentEnd="true"
                    android:layout_marginEnd="10dp"
                    android:layout_marginTop="18dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:gravity="center" />

                <Button
                    android:id="@+id/back_arrow_search"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_marginTop="18dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:visibility="invisible" />

                <Button
                    android:id="@+id/nav_button"
                    android:layout_width="35dp"
                    android:layout_height="35dp"
                    android:layout_marginTop="18dp"
                    android:clickable="true"
                    android:focusable="true" />

                <EditText
                    android:id="@+id/section"
                    android:layout_width="230dp"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="30dp"
                    android:layout_toEndOf="@+id/nav_button"
                    android:layout_toRightOf="@+id/nav_button"
                    android:background="@android:color/transparent"
                    android:clickable="false"
                    android:ems="10"
                    android:focusable="false"
                    android:gravity="left"
                    android:inputType="textMultiLine|textNoSuggestions"
                    android:maxHeight="140dp"
                    android:scrollbars="vertical"
                    android:textSize="20sp" />

                <EditText
                    android:id="@+id/searchText"
                    android:layout_width="230dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/section"
                    android:layout_alignBottom="@+id/section"
                    android:layout_alignStart="@+id/section"
                    android:ems="10"
                    android:hint="Search"
                    android:imeOptions="actionSearch"
                    android:inputType="textMultiLine|textNoSuggestions|text"
                    android:maxHeight="140dp"
                    android:textCursorDrawable="@null"
                    android:textSize="20sp"
                    android:visibility="invisible" />
            </RelativeLayout>
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>


    <FrameLayout
        android:id="@+id/progressBarLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridView
            android:id="@+id/gridView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginEnd="5dp"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:alwaysDrawnWithCache="true"
            android:clipChildren="true"
            android:columnWidth="90dp"
            android:gravity="center"
            android:horizontalSpacing="5dp"
            android:numColumns="auto_fit"
            android:padding="0dp"
            android:scrollingCache="true"
            android:smoothScrollbar="true"
            android:stretchMode="columnWidth"
            android:verticalSpacing="5dp" />
    </FrameLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navView"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:visibility="invisible">


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="70dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="left"
                android:gravity="left"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Sections"
                android:textSize="30dp" />

        </LinearLayout>
    </android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>

<ProgressBar
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center"
    android:indeterminate="true" />

</RelativeLayout>

【讨论】:

  • 感谢您的宝贵时间,但它不起作用,它仍然没有显示任何内容。
  • 您是否尝试放置整个 XML 代码并运行应用程序?因为它非常适合我
  • 哦,是的,它确实有效,但不知何故,gridview 不再显示,但我可以单击 gridview 中的项目。就是这部分 progressBar.getIndeterminateDrawable().setColorFilter(0xF09D00, PorterDuff.Mode.MULTIPLY);当我使用你的 xml 文件时,它没有显示出来。
  • 请注意,我从您的 XML 中删除了所有颜色和字符串资源,以便可以使用它。在开始时删除 GridView 周围的框架布局,让您的布局更简单一点,而不是那么嵌套
  • 是的,我重新添加了它们,除了 gridview 之外,一切都一样,它上面的项目没有显示,但我可以点击它们。 hastebin.com/ucahuxucuj.xml
【解决方案2】:

在你的 ProgressBar 中添加这样的尺寸:

android:maxHeight="20dp"
android:maxWidth="20dp"
android:minHeight="20dp"
android:minWidth="20dp"

【讨论】:

  • 不,不这样做。
猜你喜欢
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多