如何在显示和保留对话框的同时进行片段交易?

时间:2014-11-15 10:21:07

标签: android android-layout android-fragments dialog

我的活动应显示带有一些选项的自定义对话框,并且根据选项应提交布局更改而不关闭对话框。可能吗?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

  

试试这个,   首先制作custom.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/cat" />
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:text="Hey there i am cat"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</RelativeLayout>

  

然后在activity_main.xml中添加以下代码,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Button" />`</RelativeLayout>`
  

然后在MainActivity.java中,

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
Button B = (Button) findViewById(R.id.button1);
B.setOnClickListener(new OnClickListener(){
    public void onClick(View arg0) {
        Dialog d = new Dialog(MainActivity.this);
        d.setContentView(R.layout.dialog);
        d.setTitle("This is custom dialog box");
        d.show();           
    }
});
}
  

多数民众赞成......