Как я могу оптимизировать этот код диалога для Android? - PullRequest
1 голос
/ 08 октября 2009

Я хочу иметь диалог, содержащий 3 просмотра 1. заголовок с черным фоном 2. некоторый основной текст на белом фоне 3. линия с 2 кнопками с серым фоном.

Проблема в том, что я хочу, чтобы цвет фона тела был БЕЛЫМ, но даже мой взгляд установил backgroundcolor на БЕЛЫЙ, кажется, есть некоторые поля сверху и снизу тела, которые получили другой цвет фона. 1003 *

       TextView title = new TextView(this);
    title.setText("This is my title");
    title.setBackgroundColor(Color.BLACK);
    title.setPadding(10, 10, 10,10);
    title.setGravity(Gravity.CENTER);
    title.setTextColor(Color.WHITE);
    title.setTextSize(20);

    TextView view = new TextView(this);
    view.setText("Lorem Ipsum blabla bla \n more bla bla aha hhahah blablalblal.");
    view.setBackgroundColor(Color.WHITE);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);
    builder.setCustomTitle(title);
    builder.setView(view);
    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    Bingo.this.finish();
               }
           })
           .setNegativeButton("No", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
               }
           });
    AlertDialog alert = builder.create();
    alert.show();
    ((View)view.getParent()).setBackgroundColor(Color.WHITE);  // <-- UGLY fix to avoid stupid margins at top and bottom of the body...

Какие-нибудь идеи, как я могу удалить последнюю строку кода "UGLY fix"?

1 Ответ

5 голосов
/ 09 октября 2009

Чтобы исправить проблему с цветом фона, я просто установил

builder.setInverseBackgroundForced(true);

так что мой полный код

View view = View.inflate(this, R.layout.tos_dialog, null);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setIcon(R.drawable.icon);
builder.setTitle("Bla bla title");
builder.setView(view);
builder.setInverseBackgroundForced(true);
builder.setPositiveButton("I agree", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.dismiss();
       }
   });
builder.setNegativeButton("I don't agree", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
          Bingo.this.finish();
       }
    });
AlertDialog alert = builder.create();
alert.show();

и раздутый xml для представления с текстом с автолинками

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/root" 
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:padding="10px"
android:textColor="#000"
android:gravity="left"
android:textSize="14px"
android:background="#FFF"
android:autoLink="all"
android:textColorLink="#00F"
android:text="bla bla http://stackoverflow.com is cool, bla bla."
/>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...