在透明PopupWindow的布局风格的使用不后台工作 [英] Styled background used in layout of transparent PopupWindow not working

查看:208
本文介绍了在透明PopupWindow的布局风格的使用不后台工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个类扩展 PopupWindow 。我设置了类的背景 @android:彩色/透明,并使用以下布局它的内容:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / tips_root
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:填充=8DIP
    机器人:方向=横向
    风格=Tips_Background?>    <的TextView
        机器人:ID =@ + ID / TextInfo中
        机器人:layout_width =0dip
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:文字=@字符串/ register_termsandconditions
        ?TextAppearance_Footer的风格= />    <的ImageButton
        机器人:ID =@ + ID / btnClose
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =1
        机器人:填充=0dip
        机器人:layout_gravity =顶|权
        机器人:SRC =@绘制/ btn_dialog_normal
        风格=@风格/ App_ImageButtonStyle/>< / LinearLayout中>

我们的想法是让像这样一个弹出窗口:

这是我用它来实现这种效果的样式如下:

 <?XML版本=1.0编码=UTF-8&GT?;
<形状的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:可见=假>    <梯度
        机器人:startColor =@彩色/ trademark_dark
        机器人:ENDCOLOR =@彩色/ trademark_darkest
        机器人:角=90/>    <行程机器人:宽=2DP机器人:颜色=#AAAAAA/>
    <角落的an​​droid:半径=10dp/>    <填充
        机器人:左=8DP
        机器人:顶部=8DP
        机器人:右=8DP
        机器人:底部=8DP/>< /形状>

事情按预期工作,除了在窗口出现,如果背景有完全没有效果完全透明的我的启动活动。

我有一个包含使用此样式应用到整个应用程序的主题是:

 <应用
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ NI_AppTheme.Trademark
    机器人:名字=NApplication>
    <活动机器人:名字=LoginActivity>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.MAIN/>
            <类机器人:名字=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
  ....

LoginActivity 是我面临这个问题的唯一活动。什么可能我会丢失?

更新:

由@imran汗介绍解决方案的工作。但是,我需要通过一个主题属性,而不是直接硬codeD引用作为背景将有所不同主题引用这个值。我想:

 <样式名称=NI_AppTheme.Trademark>
    <项目名称=Tips_Background> @绘制/ application_tips_trademark< /项目>
< /风格>

,然后设置布局的android:背景=?Tips_Background,但它崩溃与下面的堆栈跟踪应用程序:

  06-25 21:09:36.518:E / AndroidRuntime(874):$ android.content.res.Resources NotFoundException:由造成资源不是绘制对象(彩色或路径):{的TypedValue T = 0X2 / D = 0x7f01002e一个= -1}
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.content.res.Resources.loadDrawable(Resources.java:1897)
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.content.res.TypedArray.getDrawable(TypedArray.java:601)
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.view.View<&初始化GT;(View.java:2785)
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.view.ViewGroup<&初始化GT;(ViewGroup.java:385)
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.widget.LinearLayout<&初始化GT;(LinearLayout.java:174)
 06-25 21:09:36.518:E / AndroidRuntime(874):在android.widget.LinearLayout<&初始化GT;(LinearLayout.java:170)


解决方案

活动是其在清单实际上是连接定义主题的唯一上下文。任何其他实例将使用系统默认主题夸大你的意见,导致了显示输出,你可能没有想到的。

背景下,什么情况下?

这就是为什么 getApplicationContext()将无法正确设置你的主题。

So I have this class that extends PopupWindow. I set the class's background to @android:color/transparent and use the following layout for it's contents:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tips_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="8dip"
    android:orientation="horizontal"
    style="?Tips_Background" >

    <TextView
        android:id="@+id/textInfo"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/register_termsandconditions" 
        style="?TextAppearance_Footer" />

    <ImageButton
        android:id="@+id/btnClose"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="0dip"
        android:layout_gravity="top|right"
        android:src="@drawable/btn_dialog_normal" 
        style="@style/App_ImageButtonStyle" />

</LinearLayout>

The idea is to get a popup like so:

The style that I use to achieve this effect is as follows:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:visible="false">

    <gradient 
        android:startColor="@color/trademark_dark"
        android:endColor="@color/trademark_darkest"
        android:angle="90" />

    <stroke android:width="2dp" android:color="#AAAAAA" />
    <corners android:radius="10dp" />

    <padding 
        android:left="8dp" 
        android:top="8dp"
        android:right="8dp" 
        android:bottom="8dp" />

</shape>

Things are working as expected, except in my startup activity where the window appears completely transparent as if the background had no effect at all.

I have the theme containing this style applied to the whole application using:

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:theme="@style/NI_AppTheme.Trademark"
    android:name="NApplication">
    <activity android:name="LoginActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
  ....

LoginActivity is the only activity in which I'm facing this problem. What could I be missing?

UPDATE:

The solution outlined by @imran khan works. However, I need to reference this value through a theme attribute rather than a direct hardcoded reference as the background will vary with themes. I tried:

<style name="NI_AppTheme.Trademark">
    <item name="Tips_Background">@drawable/application_tips_trademark</item>
</style>

And then set the layouts' android:background="?Tips_Background", but it crashes the application with the following stack trace:

 06-25 21:09:36.518: E/AndroidRuntime(874): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f01002e a=-1}
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.content.res.Resources.loadDrawable(Resources.java:1897)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.view.View.<init>(View.java:2785)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.view.ViewGroup.<init>(ViewGroup.java:385)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.widget.LinearLayout.<init>(LinearLayout.java:174)
 06-25 21:09:36.518: E/AndroidRuntime(874):     at android.widget.LinearLayout.<init>(LinearLayout.java:170)

解决方案

Activity is the only Context on which the themes defined in your manifest are actually attached. Any other instance will use the system default theme to inflate your views, leading to a display output you probably didn’t expect.

Context, What Context?

This is why getApplicationContext() will not properly set your theme.

这篇关于在透明PopupWindow的布局风格的使用不后台工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆