AlertDialog custom title has black border

人盡茶涼 提交于 2020-01-01 11:00:09

问题


I have an AlertDialog that I use a custom dialog view with. The idea of the custom title view seems simple enough, but there is a black border around the custom title that I can't seem to get rid of. The top, left and right sides have a single-pixel border, while the bottom side has about a 5 pixel border.

Creating the dialog in Java:

View titleView = inflater.inflate(R.layout.part_list_item, parent, false);
((TextView) titleView.findViewById(R.id.partName)).setText(titleText);
AlertDialog productDialog = new AlertDialog.Builder(getContext())
    .setCustomTitle(titleView)
    .setAdapter(adapter, doNothingClickListener)
    .create();

Custom title view layout, part_list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#ff0000"
    android:id="@+id/partName"
    android:layout_marginLeft="6dip"
    android:textAppearance="?android:attr/textAppearanceLargeInverse"
    />

What I see:

What I want to see:

Any ideas?


回答1:


Try this:

LayoutInflater inflater = (LayoutInflater)yourClass.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View titleView = inflater.inflate(R.layout.custom_dialog, null);

((TextView) titleView.findViewById(R.id.partName)).setText("Your Title");
alert1.setCustomTitle(titleView);



回答2:


This is the result created by the android when you have a title on the alert. From what i can see of the screen-shots, the "body" of the alert is also a custom view and not the alert message property.

So the easiest way to have the result you want is to add the title layout in the custom view of the alert.

example:

View titleView = inflater.inflate(R.layout.part_list_item, parent, false);

View bodyView = ....
bodyView.addview(titleView);
((TextView) itleView.findViewById(R.id.partName)).setText(titleText); 

AlertDialog productDialog = new AlertDialog.Builder(getContext());
productDialog.setView(bodyView);
...

productDialog.create();

Where the bodyView.addview(titleView); adds the title layout on your body of the alert.

And the productDialog.setView(bodyView); sets the custom view as the body of your alert.



来源:https://stackoverflow.com/questions/10222241/alertdialog-custom-title-has-black-border

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!