AlertDialog custom title has black border

2019-04-09 07:08发布

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:

screenshot broken

What I want to see:

screenshot fixed

Any ideas?

2条回答
Evening l夕情丶
2楼-- · 2019-04-09 07:13

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.

查看更多
3楼-- · 2019-04-09 07:37

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);
查看更多
登录 后发表回答