как да дефинирате позицията на диалогов прозорец за предупреждение в изглед

Възможен дубликат:
Показване на диалоговия прозорец за предупреждение в всяка позиция на екрана

Създавам приложение, в което показвам alert dialog. Искам да използвам custom layout за моя диалогов прозорец за предупреждение. Как мога да задам позицията му според моите нужди в view/layout.

Моят код:

LayoutInflater inflater=getLayoutInflater();
View view = inflater.inflate(R.layout.actionbarmain,null);
view.setMinimumWidth(200);
view.setMinimumHeight(400);

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setView(view);
alertDialog.show();

person Naseeb Sheoran    schedule 26.07.2012    source източник
comment
Разгледайте този въпрос и приетия отговор.   -  person thegrinner    schedule 26.07.2012
comment
компилаторът показва problwm в getWindow() и requestWindowFeature(Window.FEATURE_NO_TITLE);   -  person Naseeb Sheoran    schedule 26.07.2012
comment
Публикувайте кода си там, където наричате това, надявам се в onCreate() от вашата дейност.   -  person Abhilasha    schedule 26.07.2012
comment
Извиквате ли getWindow() преди show()?   -  person thegrinner    schedule 26.07.2012
comment
Моля, кажете ми как да премахна този проблем. Дори имам импортирани android.view.Window; android.view.WindowManager;   -  person Naseeb Sheoran    schedule 26.07.2012
comment
да, извиквам го преди show()   -  person Naseeb Sheoran    schedule 26.07.2012
comment
Трябва да се обадите на getWindow() преди setContentView(). Това ще реши проблема ви.   -  person Abhilasha    schedule 26.07.2012
comment
Методът requestWindowFeature(int) е недефиниран за типа AlertDialog.Builder   -  person Naseeb Sheoran    schedule 26.07.2012
comment
Методът getWindow() е недефиниран за типа AlertDialog.Builder, това е проблемът с getWindow()   -  person Naseeb Sheoran    schedule 26.07.2012
comment
Моля, кажете ми решението или някакъв алтернативен начин за разрешаване на този проблем   -  person Naseeb Sheoran    schedule 26.07.2012
comment
какво искаш да покажеш в диалога си??   -  person AkashG    schedule 26.07.2012
comment
@AkashG, моля, помогнете ми, сър, ако можете   -  person Naseeb Sheoran    schedule 26.07.2012
comment
Колко бутона има в линейно оформление и каква е ориентацията??   -  person AkashG    schedule 26.07.2012


Отговори (2)


Създайте дейност с необходимото ви оформление и задайте тема като диалогов прозорец в манифеста.

Това е пример: трябва да го промените според вашите изисквания

public class EditPropertyActivity extends Activity {

    /** Called when the activity is first created. */
    // @Override
    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog);
        setTitle("");
        TextView propName = (TextView) findViewById(R.id.tv_propName);          

        propName.setText(getIntent().getExtras().getString("prop_label"));

        final EditText ed_propValue = (EditText) findViewById(R.id.ed_propValue);
        ed_propValue.setText(value);
        if (tagName.equals("timeout")) {
            ed_propValue.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
        Button btn_save = (Button) findViewById(R.id.btn_save);
        btn_save.setOnClickListener(new View.OnClickListener() {
        //onclick
        }
    }

В манифеста:

        <activity
            android:name="EditPropertyActivity"
            android:theme="@android:style/Theme.Dialog" >
        </activity>

Оформление dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_propName"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="25dip"
        android:layout_marginTop="10dip"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/ed_propValue"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_save"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/lbl_btn_save" />

        <Button
            android:id="@+id/btn_cancel"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/cancel" />
    </LinearLayout>

</LinearLayout>
person Abhilasha    schedule 26.07.2012
comment
Благодаря за усилията, госпожо, но това не е решението на моя проблем. получавам същия проблем отново и отново - person Naseeb Sheoran; 26.07.2012
comment
Добре тогава благодаря за усилията - person Naseeb Sheoran; 26.07.2012

за да добавите персонализирано оформление в диалоговия прозорец за предупреждение, трябва да раздуете оформлението в него:

testxml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
            <Button
                android:id="@+id/btn2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
            <Button
                android:id="@+id/btn3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="test" />
</LinearLayout>

Ако искате да раздуете този xml в диалоговия прозорец за предупреждение, отколкото:

                AlertDialog.Builder builder=new Builder(YourActivityName.this);
                LayoutInflater inflater=getLayoutInflater();
                View view=inflater.inflate(R.layout.testxml, null);
                builder.setView(view);
                builder.setTitle("New Project");
                builder.setMessage("knknknknknmknknknk");
                builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.create();
                builder.show();
person AkashG    schedule 26.07.2012
comment
Току-що взех линейно оформление с 3 бутона с вертикална ориентация, можете да добавите колкото се може повече бутони към него и да промените ориентацията според вашите изисквания. Приложих това, за да изчистя проблема ви и да задам това оформление като изглед в AlertDialog. - person AkashG; 26.07.2012