【问题标题】:how to define the position of a alert dialog in view [duplicate]如何在视图中定义警报对话框的位置[重复]
【发布时间】:2012-07-26 11:49:53
【问题描述】:

可能重复:
Show AlertDialog in any position of the screen

我制作了一个应用程序,其中显示了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();

【问题讨论】:

  • 看看this问题和接受的答案。
  • 编译器在 getWindow() 和 requestWindowFeature(Window.FEATURE_NO_TITLE) 中显示问题;
  • 将您的代码发布在您调用的位置,我希望在您的活动的onCreate() 中。
  • 你是在 show() 之前调用 getWindow() 吗?
  • 请告诉我如何解决这个问题。即使我已经导入了android.view.Window; android.view.WindowManager;

标签: android android-alertdialog


【解决方案1】:

使用您所需的布局创建一个活动,并将主题指定为清单中的对话框。

这是一个示例:您需要根据您的要求进行更改

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>

布局对话框.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>

【讨论】:

  • 感谢您的努力女士,但这不是我的问题的解决方案。我一次又一次遇到同样的问题
  • 好的,谢谢你的努力
【解决方案2】:

要在警报对话框中添加自定义布局,您必须在其中膨胀布局:

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();

【讨论】:

  • 我刚刚采用线性布局,具有 3 个方向垂直相同的按钮,您可以向其添加尽可能多的按钮,并可以根据您的要求更改方向。我实现了这个以清除您的问题并设置此布局作为 AlertDialog 中的视图。
猜你喜欢
  • 2021-05-31
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 2022-01-20
  • 2022-01-10
相关资源
最近更新 更多