Android 主题样式

2017-10-11  本文已影响87人  act262

attrs.xml文件中声明属性名

比如Theme范围内定义的属性toastFrameBackground(Toast的背景)

<declare-styleable name="Theme">
    <!-- Background to use for toasts -->
    <attr name="toastFrameBackground" format="reference" />
    
</declare-styleable>

theme.xml

  <style name="Theme">
       <!-- Toast attributes -->
        <item name="toastFrameBackground">@drawable/toast_frame</item>
        在这里定义了该属性的值
   </style>

    <!-- Theme for a window that looks like a toast. -->
    <style name="Theme.Toast" parent="Theme.DeviceDefault.Dialog">
        在这里使用了该定义的值
        <item name="windowBackground">?attr/toastFrameBackground</item>
        <item name="windowAnimationStyle">@style/Animation.Toast</item>
        <item name="backgroundDimEnabled">false</item>
        <item name="windowCloseOnTouchOutside">false</item>
        <item name="windowContentTransitions">false</item>
        <item name="windowActivityTransitions">false</item>
    </style>

Toast

        // 说明我们默认的Toast并没有直接使用这个"Theme.Toast",而是代码中动态设置一些属性,效果是一样的
        TN() {
            // XXX This should be changed to use a Dialog, with a Theme.Toast
            // defined that sets up the layout params appropriately.
            final WindowManager.LayoutParams params = mParams;
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
            params.width = WindowManager.LayoutParams.WRAP_CONTENT;
            params.format = PixelFormat.TRANSLUCENT;
            params.windowAnimations = com.android.internal.R.style.Animation_Toast;
            params.type = WindowManager.LayoutParams.TYPE_TOAST;
            params.setTitle("Toast");
            params.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                    | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
        }

实际中Toast用到的是在他的布局中用到的
transient_notification.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="?android:attr/toastFrameBackground">

    <TextView
        android:id="@android:id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        android:textAppearance="@style/TextAppearance.Toast"
        android:textColor="@color/bright_foreground_dark"
        android:shadowColor="#BB000000"
        android:shadowRadius="2.75"
        />

</LinearLayout>
上一篇下一篇

猜你喜欢

热点阅读