java - Android 中不显示对话框标题

标签 java android xml android-studio dialog

我正在尝试创建一个对话框。所有项目都会出现并且它会响应触摸,但标题不会出现。我检查了其他答案,他们建议在 xml 中将“windowNoTitle”设置为“false”,但这没有帮助。构建对话框后也没有尝试“setTitle”(这就是现在的写法)。为什么我的标题没有出现?

这是 MainActivity 的代码:

package samapps.myfirstapp;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

public class MainActivity extends AppCompatActivity implements DialogTest.OnFragmentInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onButtonClick(int result){
        Log.d("tag","Executed on click");
    }

    //Called when the Send button is tapped
    public void sendMessage(View view){
       String[] text = {"Test 1","Test 2","Test 3"};
        String title = "Test Title";
        DialogFragment fragment = new DialogTest();
        Bundle args = new Bundle();
        args.putString("title_question",title);
        args.putStringArray("choices",text);
        fragment.setArguments(args);
        fragment.show(getSupportFragmentManager(),"testtest123");
        Log.d("tag","Executed after dialog");
}

}

以及对话框 fragment :

package samapps.myfirstapp;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;



public class DialogTest extends DialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    public DialogTest() {
        // Required empty public constructor
    }

    public interface OnFragmentInteractionListener {
        public void onButtonClick(int result);
    }



    @Override
    public Dialog onCreateDialog (Bundle savedInstanceState){
        Bundle theBundle = this.getArguments();

        String[] choices = theBundle.getStringArray("choices");
        String choiceTitle = theBundle.getString("choice_title");

        AlertDialog.Builder builder;
        if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Material_Dialog_Alert);
        } else {
            builder = new AlertDialog.Builder(getActivity().getParent());
        }
        builder.setTitle(choiceTitle);

        builder.setItems(choices,new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog,int which){
                mListener.onButtonClick(which);
            }
        });

        Dialog thisDialog = builder.create();
        thisDialog.setTitle(choiceTitle);
        return thisDialog;
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

}

就其值(value)而言,这是fragment_dialog_test.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="samapps.myfirstapp.DialogTest"
    android:windowNoTitle="false"
    style="@style/CustomDialog">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

在“Styles.xml”中使用此内容

<style name="CustomDialog" parent="Theme.AppCompat.Dialog">
    <item name="windowNoTitle">false</item>
</style>

最佳答案

在您的DialogFragment中,您使用了错误的KEY来获取标题theBundle.getString("choice_title")。尝试使用title_question

用途:

@Override
public Dialog onCreateDialog (Bundle savedInstanceState){
    Bundle theBundle = this.getArguments();

    String[] choices = theBundle.getStringArray("choices");
    String choiceTitle = theBundle.getString("title_question");
    ...........
}

关于java - Android 中不显示对话框标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43245116/

相关文章:

java - Android - 打开 session 不适用于最新的 Facebook 应用程序版本,如果未安装 Facebook,则工作正常

java - MySQL--字段列表中的未知列

android - 仅将视频加载到 VideoView 中而不立即播放

android - 在 android 上使用 ContactsContract api 从联系人获取邮政地址

java - Android JDOM2 xml解析,IOExcetion

xml - 在 Android 上编写 XML

java - GUI 中的 JTables 出现问题

java - 在 Maven 插件中使用属性文件中的变量

android - 无法将 int 与 SQLite put 方法一起使用,编译器错误,Android

xml - 为了验证而不是我们的 XML 来构建自己的 XML 模式是个好主意吗?