就是在代码中设置setTitle,不要在布局文件中写死,我现在这个就写死了。
java代码:dialog.setTitle("你在哪?"); 这样设置了,不行啊!
- Java code
public class MainActivity extends Activity { /** Called when the activity is first created. */ Dialog dialog; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //new一个Dialog对象,在实例化的时候传入主题 dialog = new MyDialog(MainActivity.this, R.style.MyDialog); //设置它的ContentView dialog.setContentView(R.layout.dialog); // 设置对话框关闭之前对话框之外的区域不可点击 dialog.setCanceledOnTouchOutside(false); dialog.setTitle("你在哪?"); dialog.show(); }
dialog.setTitle("你在哪?"); 这样设置了,不行啊!
- Java code
public class MyDialog extends AlertDialog { Context context; public MyDialog(Context context) { super(context); // TODO Auto-generated constructor stub this.context = context; } public MyDialog(Context context, int theme){ super(context, theme); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.setContentView(R.layout.dialog); ImageView imageview = (ImageView)findViewById(R.id.imageviewClose); imageview.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { cancel(); } }); }
布局文件:
- Java code
<?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" android:background="#FFFFFF" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="12dip" android:paddingTop="6dip" android:textColor="#050505" android:textSize="15dp" android:text="用户" /> <ImageView android:id="@+id/imageviewClose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginRight="15dp" android:src="@drawable/close" /> </RelativeLayout> <View android:layout_width="fill_parent" android:layout_height="2px" android:layout_marginTop="5dp" android:background="@drawable/mypic" /> </LinearLayout>
------解决方案--------------------
把
Dialog dialog;
改为
MyDialog dialog;
------解决方案--------------------
你这个是自定义风格的dialog就不能用setTitle,在布局里面添加Title位置,并在MyDialog添加setTitle(String title)方法将字符串title显示到Title位置。