【问题标题】:Android spinner arrow always above other viewsAndroid 微调器箭头始终位于其他视图上方
【发布时间】:2021-08-15 16:19:49
【问题描述】:

在我的应用程序中,我在滚动视图中有一些微调器(每个微调器都是片段的一部分)。此滚动视图位于 textView 下方。

问题是,当我在我的测试设备(Samsung Galaxy S6 Edge [G925F],API 23)上运行应用程序时,微调器的小箭头在 textView 上方保持可见,即使其余的微调器已经不在视线范围内。

我做了很多研究,但找不到任何解决方案,所以我在这里问: 我需要怎么做才能使箭头像旋转器的其余部分一样消失?

这里有一些图片:

在这张图片中,在我向下滚动之前,您可以在 Activity 顶部和微调器下方看到 textView

在此屏幕截图中,您可以看到问题所在。微调器的小三角形仍然可见,但微调器本身已经滚动到视野之外。

这是包含微调器的片段的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/fragLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/gradeDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dp"
        android:layout_marginLeft="15dp"
        android:text="1"
        android:textSize="30dp"/>
    <Spinner
        android:id="@+id/spinner"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="30dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:entries="@array/grades"/>
    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginEnd="15dp"
        android:layout_marginRight="15dp"
        android:src="@drawable/ic_remove_black_24dp"
        android:background="@null"/>
</LinearLayout>
<Space
    android:layout_width="0dp"
    android:layout_height="5dp"
    android:layout_below="@id/fragLayout"/>
</RelativeLayout>

这里是 MainActivitys 内容的代码:

<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="de.jeanma.android.avo.MainActivity"
tools:showIn="@layout/activity_main">
    <Space
        android:id="@+id/mainContentSpace"
        android:layout_width="match_parent"
        android:layout_height="16dp" />
    <TextView
        android:id="@+id/textView"
        android:layout_below="@id/mainContentSpace"
        android:layout_width="match_parent"
        android:text="Standard text"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:gravity="end"/>
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_below="@id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/layout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:divider="@drawable/divider"
            android:showDividers="middle">
        </LinearLayout>
    </ScrollView>
</RelativeLayout>

片段通过 Java 方法添加到 LinearLayout (@id/layout)。如果需要,我也会在这里发布。

我用的是Java 1.8.05,目标SDK是23,最低是14,代码是用Android Studio写的

有问题的textView是@id/textView,顶部显示大号。

【问题讨论】:

  • 这很奇怪。 0,0 是文本视图吗?它是什么 id 以及它在哪个布局中?
  • 这不是答案,而是:您是否为该滚动视图中的每个微调器添加一个片段?是不是有点矫枉过正?应该使用简单的 ListView 或 RecyclerView 更好,除非你真的需要这么多片段。
  • 是的,这就是 textView。它的 id 是@id/textView。 @ScriptKitty
  • 是的,但是该片段还包含一个按钮和一个 TextView。我需要可变数量的它们。工厂正在添加它们。@Wukash
  • @Jeanma Wukash 的提示仍然有效。列表视图可以采用自定义布局。至于你的问题,如果这可行,我必须先把它放在我的电脑上,但由于它是线性布局,你可以降低滚动视图的高度吗?

标签: android android-spinner


【解决方案1】:

@jeanma 您只需设置即可解决问题

android:background="#00FFFFFF"

为您的 ScrollView 或 ScrollView 的根 LinearLayout

【讨论】:

    【解决方案2】:

    我现在有一个解决方法正在运行。正如我将 textView 的背景颜色设置为与我的活动相同的建议,箭头不再显示。但空间确实表现不同。在那里它改变了我改变背景颜色的东西。所以我只是删除了它。 (看起来还是不错的)。

    可以在这里看到更新后的 XML 文件和 MainActivity'a 的内容:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="de.jeanma.android.avo.MainActivity"
    tools:showIn="@layout/activity_main">
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:text="Standard text"
            android:layout_height="wrap_content"
            android:textSize="40dp"
            android:gravity="end"
            android:background="#FAFAFA"/>
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView">
            <LinearLayout
                android:id="@+id/layout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:divider="@drawable/divider"
                android:showDividers="middle">
            </LinearLayout>
        </ScrollView>
    </RelativeLayout>
    

    但是因为箭头显示也抛出了填充,我改变了我的

    src\main\res\values\dimens.xml 如下(我知道这是非常糟糕的做法):

    <resources>
        <!-- Default screen margins, per the Android Design guidelines. -->
        <dimen name="activity_horizontal_margin">16dp</dimen>
        <dimen name="activity_vertical_margin">0dp</dimen><!-- This was changed from 16dp to 0dp -->
        <dimen name="fab_margin">16dp</dimen>
    </resources>
    

    感谢@ScriptKity @Wukash @Bryan 的支持

    【讨论】:

    • 你能看看这个stackoverflow link 吗?使用包含 Spinners 的项目时,我遇到了几乎相同的问题。谢谢!
    【解决方案3】:

    在父布局中设置背景颜色。

        android:background="@color/white"
    

    【讨论】:

      【解决方案4】:

      我最近遇到了这个问题,并且认为这是结合 ScrollView 和 Spinner 时在 Android 上的硬件加速渲染中的一个错误。

      我可以通过关闭 Spinner 的硬件加速来解决这个问题:

      mySpinner.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
      

      这里描述了如何控制硬件加速的其他可能性:

      https://developer.android.com/guide/topics/graphics/hardware-accel

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2016-12-10
        • 1970-01-01
        相关资源
        最近更新 更多