【问题标题】:App name showing on top of Alert Activity popup应用程序名称显示在警报活动弹出窗口顶部
【发布时间】:2017-03-09 10:17:41
【问题描述】:

当从托盘中选择通知时,我会显示弹出警报活动。 但最重要的是它显示了应用程序名称 (MCP),但对齐不正确。

我不想要顶部,只是一个普通的对话框。

清单:

<activity
    android:name=".activity.AlertDialogActivity"
    android:configChanges="orientation|locale|screenSize|uiMode|fontScale"
    android:excludeFromRecents="true"
    android:theme="@style/AlertDialogTheme" />

风格:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

警报活动:

public class AlertDialogActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_alert_dialog);
    this.setFinishOnTouchOutside(false);

    String message = getIntent().getExtras().getString("message");

    TextView tvPushMessage = (TextView) findViewById(R.id.tvAlertMessage);
    tvPushMessage.setText(message);

    Button btnPushOk = (Button) findViewById(R.id.btnAlertOk);
    btnPushOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:background="@drawable/background_round_rectangle"
    android:padding="@dimen/activity_vertical_margin">
    <TextView
        android:id="@+id/tvAlertMessage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dp"
        android:text="Message"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@color/black" />;
    <Button
        android:id="@+id/btnAlertOk"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_below="@id/tvAlertMessage"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:background="@drawable/btn_use"
        android:text="@string/ok"
        android:textColor="@color/white" />
</RelativeLayout>`

也试过了:

requestWindowFeature(Window.FEATURE_NO_TITLE);

和:

getWindow().setFeatureDrawableResource(Window.FEATURE_NO_TITLE, android.R.drawable.ic_dialog_alert)

setContentView()之前

【问题讨论】:

  • 你试过不带android:前缀吗,如下:name="windowNoTitle"
  • 我现在做了,它成功了,你能解释一下区别吗?
  • 这与您的 api 级别有关:“由于 Android 主题系统的限制,任何主题自定义都必须在两个属性中声明:普通的 android-prefixed 属性将主题应用于原生样式(API +1 ) 和无前缀属性用于自定义实现 (API +11)”。更多精度请参见this answer(对于 ABS,但与 AppCompat 相同)。

标签: android android-activity alert android-alertdialog


【解决方案1】:

只需在 style.xml 中添加以下代码
<item name="windowNoTitle">true</item> <item name="windowActionBar">false</item>

或者您可以通过编程方式隐藏它。

getSupportActionBar().hide();

【讨论】:

  • 第一项工作。正如artman 和Fllo 所提到的,不需要放置“android”。 'getSupportActionBar().hide()' 得到 NullPointerException。
【解决方案2】:

试试这个代码(不使用“android:”)

<style name="Theme.MyDialog" parent="@style/Theme.AppCompat.Dialog">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

【讨论】:

    【解决方案3】:

    我用过这个:

    在AppCompatActivity onCreate()中添加如下代码:

    setTitle("")
    

    并在style.xml中添加以下代码

    <style name="PopupWindowTheme" parent="@style/Theme.AppCompat.Dialog.Alert">
        <item name="android:windowBackground">@color/transparent</item>
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowActionBar">false</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowAnimationStyle">@style/AnimBottom</item>
    </style>
    

    【讨论】:

      【解决方案4】:

      以这种方式添加 requestWindowFeature 可能会正常工作。

      添加后首先调用Super。

      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              setContentView(R.layout.add__favourite_layout);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-29
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 2015-07-09
        • 1970-01-01
        相关资源
        最近更新 更多