Android - 设置对话框片段的样式

时间:2016-11-09 08:49:02

标签: android styles fragment dialogfragment

我想在它周围创建带有黑色windowBackground的片段对话框,但是当我添加一个样式时,我在片段周围有一个黑框。有人知道为什么这个框架出现在我的片段中:

Fragment example

这是我的片段xml:

  `  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/tocLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/clean_backgroung_pop_up_ltr"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/settings_border"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/title_layout_settings"
            style="@style/reader_dialog_title">

            <TextView
                android:id="@+id/title_txt_settings"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_weight="4"
                android:gravity="center"
                android:text="@string/menu_settings"
                android:textColor="@color/white_color"
                android:textSize="@dimen/global_text_size" />

            <ImageView
                android:id="@+id/imvClose"
                android:layout_width="@dimen/global_close_button_width"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical|right"
                android:layout_weight="1"
                android:padding="@dimen/global_padding"
                android:src="@drawable/icon_close_window" />

            <ImageView
                android:id="@+id/title_txt_settings_icon"
                android:layout_width="@dimen/global_image_width"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical|left"
                android:layout_weight="1"
                android:padding="@dimen/global_padding"
                android:scaleType="center"
                android:src="@drawable/toolbar_settings_button_heb" />
        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:paddingLeft="@dimen/global_margin_s"
            android:paddingRight="@dimen/global_margin_s">

            <FrameLayout
                android:id="@+id/tts_layout_settings"
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="@dimen/settings_screen_layout_height"
                android:gravity="center_horizontal"
                android:longClickable="true">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/settings_screen_seek_bar_hight"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:background="@drawable/tts_rabbit_turtle"
                    android:gravity="center_horizontal" />

                <SeekBar
                    android:id="@+id/tts_speed_settings_seek"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/global_book_reader_menus_margin_l"
                    android:layout_marginRight="@dimen/global_book_reader_menus_margin_l"
                    android:max="4"
                    android:maxHeight="1dp"
                    android:progressDrawable="@drawable/seek_bar"
                    android:thumb="@drawable/sliderknob_23" />
            </FrameLayout>

            <View
                android:id="@+id/setting_seperator"
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="1dp"
                android:layout_below="@id/tts_speed_settings_seek"
                android:background="#CECECE"
                android:drawablePadding="@dimen/global_margin_s" />

            <FrameLayout
                android:id="@+id/brightness_layout_settings"
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="@dimen/settings_screen_layout_height">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/settings_screen_seek_bar_hight"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:background="@drawable/bright"
                    android:gravity="center_horizontal" />

                <SeekBar
                    android:id="@+id/brightness_settings_seek"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginLeft="@dimen/global_book_reader_menus_margin_l"
                    android:layout_marginRight="@dimen/global_book_reader_menus_margin_l"
                    android:backgroundTint="@color/application_gray"
                    android:maxHeight="1dp"
                    android:progressDrawable="@drawable/seek_bar"
                    android:thumb="@drawable/sliderknob_23" />
            </FrameLayout>

            <View
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="1dp"
                android:layout_below="@+id/brightness_settings_seek"
                android:background="#CECECE"
                android:drawablePadding="@dimen/global_margin_s"
                android:gravity="center" />


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/settings_screen_sub_layout_height">

                <Switch
                    android:id="@+id/switchHighlights"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:text="@string/show_highlights_info"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/settings_text_color"
                    android:id="@+id/txvShowHighligtsInfo" />
            </FrameLayout>

            <View
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="1dp"
                android:layout_below="@+id/brightness_settings_seek"
                android:background="#CECECE"
                android:drawablePadding="@dimen/global_margin_s"
                android:gravity="center" />


            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/settings_screen_sub_layout_height">

                <Switch
                    android:id="@+id/switchTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical" />

                <TextView
                    android:id="@+id/txvNightTheme"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:text="@string/search_popup_night_theme_txt"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/settings_text_color" />
            </FrameLayout>

            <View
                android:layout_width="@dimen/settings_screen_popover_width"
                android:layout_height="1dp"
                android:layout_below="@+id/brightness_settings_seek"
                android:background="#CECECE"
                android:drawablePadding="@dimen/global_margin_s"
                android:gravity="center" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/settings_screen_sub_layout_height">

                <Switch
                    android:id="@+id/switchLockScreen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right|center_vertical" />

                <!--       <android.support.v7.widget.SwitchCompat
                           android:id="@+id/switchLockScreen"
                           android:layout_width="153dp"
                           android:layout_height="wrap_content"
                           android:layout_gravity="center_vertical|left"
                           android:theme="@style/MySwitch" />   -->

                <TextView
                    android:id="@+id/txvLockScreen"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left|center_vertical"
                    android:text="@string/search_popup_lock_screen_txt"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@color/settings_text_color" />
            </FrameLayout>


        </LinearLayout>


    </LinearLayout>

    <!-- Font seek bar  -->

    <!-- Brighness -->

    <!--Night mode-->


</LinearLayout>`

这是我的风格:

<style name="MyDialog" parent="@android:style/Theme.DeviceDefault.Light.Dialog"> <item name="android:windowBackground">@color/trans_black_second</item> </style>

这是我在DialogFragment中设置它的方式:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i(TAG,"BookDialog onCreate");
    setCancelable(true);
    if ( mBookReader != null && inflater == null) {
        inflater = (LayoutInflater) mBookReader.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mWindowManager = (WindowManager)mBookReader.getSystemService(Context.WINDOW_SERVICE);
    }
    tfOpenSansHebrew =  FontUtils.getFont(getActivity(), FontUtils.FONT_OPEN_SANS);

    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.MyDialog);

}

1 个答案:

答案 0 :(得分:0)

您的对话框的背景已经引用了行android:background="@drawable/clean_backgroung_pop_up_ltr。如果要将其更改为暗色,则必须修改drawable。您可以以编程方式应用转换,或者只使用第一个作为基础创建另一个drawable。

另外,不要忘记将对话框主题更改为Theme.DeviceDefault.Dialog,否则对话框中的元素在深色背景上看起来会很差。

现在发生的事情是窗口的背景被绘制为黑色矩形,然后布局的背景被绘制在它上面。当然,您可以确保窗口背景与布局的背景具有相同的形状,但在这种情况下,它将完全隐藏在后者的下方,因此绝对没用。