如何使ScrollView透明?

时间:2018-08-20 18:29:19

标签: android android-layout android-styles android-scrollview

我有一个包含ConstraintLayout的ScrollView。 ConstraintLayout的背景为可绘制资源:android:background=@drawable/bg_rounded_corner_pin_bg,成功为ConstraintLayout设置了圆角。我的问题是ScrollView出现在ConstraintView的圆角后面,而我无法使ScrollView透明:

这是布局文件:

activity_enterpin.xml

<?xml version="1.0" encoding="utf-8"?>       
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:id="@+id/scrollviewPin"
    android:background="#00000000" // this is where I need transparency
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:maxWidth="@dimen/layout_maxwidth"
        android:padding="@dimen/layout_padding"
        android:background="@drawable/bg_rounded_corner_pin_bg">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/title_margintop"
            android:gravity="center"
            android:text="@string/pinlock_title"
            android:textColor="@color/text_title"
            android:textSize="@dimen/textsize_title"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <com.example.isaacmain3.lockscreen.andrognito.pinlockview.IndicatorDots
            android:id="@+id/indicator_dots"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/dot_margintop"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title" />

        <TextView
            android:id="@+id/attempts"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/attempts_margintop"
            android:textColor="@color/text_attempts"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/indicator_dots" />

        <com.example.isaacmain3.lockscreen.andrognito.pinlockview.PinLockView
            android:id="@+id/pinlockView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/lockview_margin"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/attempts"
            app:layout_constraintVertical_bias="0.1" />

    </android.support.constraint.ConstraintLayout>
</ScrollView>

这是目前的结果:

enter image description here

我已经在ScrollView中使用了背景色,并且能够从上面我注释过的字段中成功地将颜色更改为任何非透明颜色。我如何摆脱这些角落?

这是我用来绘制圆角的可绘制文件,以防万一:

bg_rounded_corner_pin_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/colorPrimaryDark"/>

  <!--  <stroke android:width="2dp"
        android:color="#999999"/>-->

  <!--  <padding android:left="2dp"
        android:top="2dp"
        android:right="2dp"
        android:bottom="2dp"/>-->

    <corners android:bottomRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        />
</shape>

2 个答案:

答案 0 :(得分:0)

代替设置背景,使用

android:scrollbarThumbVertical =“ @ null”

答案 1 :(得分:0)

styles.xml中定义一个新主题:

<style name="TransparentActivity" parent="@android:style/Theme.DeviceDefault.Light.NoActionBar">
    <item name="android:windowBackground">@drawable/bg_rounded_corner_pin_bg</item>
    <item name="android:windowIsTranslucent">true</item>
</style>

然后,在AndroidManifest.xml中:

<activity android:name=".<activity_name>"
        android:theme="@style/TransparentActivity"/>

最后,在活动类中:

DisplayMetrics m = new DisplayMetrics();
getDisplay().getMetrics(m);

WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = m.widthPixels*4/5;
params.height = m.heightPixels*3/4;
params.gravity = Gravity.CENTER;
getWindow().setAttributes(params);

super.onCreate(savedInstanceState);
....

请注意,在调用super.onCreate()之前,有代码。