Android中的透明底部布局

时间:2016-05-28 14:42:57

标签: android android-layout

我正在尝试制作一个透明的底部工作表布局,这样我就可以看到它下方视图的内容。底部工作表按预期工作,但是当我将背景设置为@null@android:color/transparent时,布局视图为白色,而不是透明。我的布局如下:

app_bar_main.xml:

<android.support.design.widget.CoordinatorLayout 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:id="@+id/coordinatorLayout"
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    tools:context=".core.activities.MainActivity">
    <!-- stuff here -->
    <LinearLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@null"
        android:orientation="vertical"
        app:layout_behavior="@string/bottom_sheet_behavior">
    </LinearLayout>

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

标识为bottom_sheet的线性布局就是我的底页。工作表本身定义如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:background="@null"
    android:layout_height="match_parent">
    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@null"
        android:orientation="vertical">

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/bottom_sheet_placeholder_layout"
            android:layout_weight="0.6"
            android:layout_width="match_parent"
            android:background="@null"
            android:layout_height="50dp"
            android:orientation="horizontal">
        </LinearLayout>
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/bottom_sheet_layout"
            android:layout_margin="0dp"
            android:layout_weight="0.4"
            android:layout_width="match_parent"
            android:background="@color/my_background"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ProgressBar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:id="@+id/my_progress_bar" />

            <TextView
                android:layout_marginTop="5dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Large Text"
                android:textColor="@color/my_text"
                android:id="@+id/txt_my_info"
                android:layout_gravity="center_horizontal"
                android:visibility="gone"
                android:textSize="48px" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="Medium Text"
                android:id="@+id/txt_my_address"
                android:textColor="@color/my_secondary_text"
                android:visibility="gone"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>
    </LinearLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/btn_edit_tree_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:layout_marginTop="-62dp"
        android:elevation="100dp"
        android:src="@drawable/ic_create_black_24dp"
        app:layout_anchor="@id/bottom_sheet_layout"
        app:layout_anchorGravity="top|end|right"
        app:useCompatPadding="true"/>
</android.support.design.widget.CoordinatorLayout>

9 个答案:

答案 0 :(得分:7)

答案要容易得多:只需添加此行

即可

myDialog .getWindow() .findViewById(R.id.design_bottom_sheet) .setBackgroundResource(android.R.color.transparent);

并且不要更改标准ID(R.id.design_bottom_sheet和android.R.color.transparent)。它使Bottom Sheet Dialog的背景透明

答案 1 :(得分:5)

Traceback (most recent call last):
  File "/Users/jamesball/Documents/workspace/Workspace/Week9.py", line 52, in <module>
    uni1.enrollStudent(stud1)         
  File "/Users/jamesball/Documents/workspace/Workspace/Week9.py", line 30, in enrollStudent
    self.registry.update(new_student.degree)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

答案 2 :(得分:2)

我必须等到在onActivityCreated中调用setContentView之后才能访问容器。同样,在getDialog().getWindow().findViewById(R.id.design_bottom_sheet)成功找到FrameLayout的同时,我决定避免使用getView().getParent()显式调用内部定义的ID。

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);  // setContentView called here
    ((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}

答案 3 :(得分:1)

在底部setupDialog(final Dialog dialog, int style)

中添加此内容
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

并将android:background="@android:color/transparent"添加到根视图

答案 4 :(得分:1)

将此添加到styles.xml

 <style name="TransparentDialog" parent="Theme.Design.Light.BottomSheetDialog">
    <item name="android:windowCloseOnTouchOutside">false</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:colorBackground">     @android:color/transparent</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:backgroundDimAmount">0.3</item>
    <item name="android:windowFrame">@null</item>
    <item name="android:windowIsFloating">true</item>
</style>


   BottomSheetDialog bsDialog = new BottomSheetDialog(this,R.style.TransparentDialog);
   bsDialog.setContentView(R.layout.bottomsheet_dialog);
   bsDialog.show();

这很有效

答案 5 :(得分:1)

这里是kolorszczak带来的kotlin版本。希望它可以节省别人转换的时间。

val dialog = BottomSheetDialog(activity!!)
 dialog.setOnShowListener {
            var dialogTmp: BottomSheetDialog = it as BottomSheetDialog
            var bottomSheet: FrameLayout? =
                dialogTmp.findViewById(R.id.design_bottom_sheet) as FrameLayout?
                    ?: return@setOnShowListener
            bottomSheet?.background = null
        }

答案 6 :(得分:0)

在color.xml中创建:<color name="colorTransparent">#00000000</color> 并使用android:background="@color/colorTransparent"

答案 7 :(得分:0)

更改颜色背景

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/bottom_sheet_placeholder_layout"
        android:layout_weight="0.6"
        android:layout_width="match_parent"
        android:background="#00FFFFFF"
        android:layout_height="50dp"
        android:orientation="horizontal">
    </LinearLayout>

试一试!!

答案 8 :(得分:0)

只需使用这种样式:

def groupSequence(lst): 
  " Creates sublists of ascending elements "
    res = [[lst[0]]] 

    for i in range(1, len(lst)): 
        if lst[i] > lst[i-1]: # test that next element is greater
            res[-1].append(lst[i]) 

        else: 
            res.append([lst[i]]) 
    return res 

a = [3, 4, 5, 7, 9, 11, 4, 5, 8, 10, 5, 8, 9]
res = sum((max(x)-min(x) for x in groupSequence(a)))
print(res) #18

,然后将样式设置为<style name="BaseBottomSheetDialogThem" parent="@style/Theme.Design.Light.BottomSheetDialog"> <item name="android:windowIsFloating">true</item> <item name="android:backgroundDimEnabled">false</item> </style>