我絕對是Android的初學者。現在我遇到了在Android中使用自定義視圖設置默認AlertDialog寬度的問題。它沒有調整警報對話框的寬度。請問我的代碼有什麼問題?無法通過Android中的自定義視圖更改AlertDialog的寬度
這是我的警告對話框
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
android:textColor="@color/white"
app:backgroundTint="@color/green"
android:layout_gravity="center_horizontal"
android:id="@+id/btn_row_option_done"
android:text="Done"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:textColor="@color/white"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="@color/lightBlue"
android:layout_gravity="center_horizontal"
android:text="Edit"
android:id="@+id/btn_row_option_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
android:textColor="@color/white"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="@color/red"
android:layout_gravity="center_horizontal"
android:text="Delete"
android:id="@+id/btn_row_option_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.AppCompatButton
xmlns:app="http://schemas.android.com/apk/res-auto"
app:backgroundTint="@color/white"
android:layout_gravity="center_horizontal"
android:text="Cancel"
android:id="@+id/btn_row_option_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
視圖佈局正如你所看到的,我設置的LinearLayout的寬度爲wrap_content。
這是我正在打開的警告對話框中在我的Java代碼
public void showOptionDialog(final int id)
{
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
View view = layoutInflater.inflate(R.layout.row_option_dialog, null);
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
Boolean isTaskDone = dbHelper.isTaskDone(id);
Button doneBtn = (Button)view.findViewById(R.id.btn_row_option_done);
if(isTaskDone==false)
{
doneBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.markAsDone(id);
refreshListView();
alertDialog.cancel();
Toast.makeText(getActivity().getBaseContext(),"Marked as done",Toast.LENGTH_SHORT).show();
}
});
}
else{
ViewGroup viewGroup = (ViewGroup)doneBtn.getParent();
viewGroup.removeView(doneBtn);
}
Button editBtn = (Button)view.findViewById(R.id.btn_row_option_edit);
if(isTaskDone==false)
{
editBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity activity = (MainActivity) getActivity();
activity.replaceEditTaskFragment(id);
alertDialog.cancel();
}
});
}
else{
ViewGroup viewGroup = (ViewGroup)editBtn.getParent();
viewGroup.removeView(editBtn);
}
Button deleteBtn = (Button)view.findViewById(R.id.btn_row_option_delete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dbHelper.deleteTask(id);
items.remove(optionFocusedItemIndex);
adapter.notifyDataSetChanged();
Toast.makeText(getActivity().getBaseContext(),"Task deleted",Toast.LENGTH_SHORT).show();
alertDialog.cancel();
updateListEmptyText();
}
});
Button cancelBtn = (Button)view.findViewById(R.id.btn_row_option_cancel);
cancelBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.cancel();
}
});
alertDialog.setView(view);
alertDialog.show();
}
但打開的警告對話框中時,其寬度不調整,仍然呈現像截圖默認大小。
這是截圖。
正如你所看到的,寬度幾乎是滿屏幕的連我自己設置爲WRAP_CONTENT。我也設置了像300px的東西。它不工作。請問什麼是錯的寬度我的代碼,我怎麼能實現它?
請問我能得到一個簡單的代碼嗎? –
http://www.mkyong.com/android/android-custom-dialog-example/這是例如自定義對話框:) – Tito
這是我的例子與活動至極就像是一個對話框。這裏是xml文件,你可以看到其他部分https://github.com/Krystev/Internship/blob/master/Android/TextEditor/app/src/main/res/layout/activity_info.xml – Tito