這怎麼可能?謝謝!我應該將View(RelativeLayout)置於另一個視圖上並使其不可見,並且當用戶單擊該按鈕時,它將其設置爲可見?或者還有另一種在屏幕中間打開對話框(?)的更簡單的方法嗎?如何在android中點擊按鈕時出現視圖
謝謝!
這怎麼可能?謝謝!我應該將View(RelativeLayout)置於另一個視圖上並使其不可見,並且當用戶單擊該按鈕時,它將其設置爲可見?或者還有另一種在屏幕中間打開對話框(?)的更簡單的方法嗎?如何在android中點擊按鈕時出現視圖
謝謝!
您需要一個佈局來放置子視圖。最安全的是線性佈局,因爲添加多個孩子很容易
Fisrt在佈局中創建一個xml文件。
用於例如,在佈局 添加一個名爲文件說iv.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_iv"
android:layout_width="200dp"
android:layout_height="120dp"
android:padding="2dp"
android:scaleType="fitCenter"
android:src="@drawable/person_default_page" />
在您要添加一些創建一個線性佈局佈局好說@ + ID/ROW_ID 然後在代碼中當您需要添加視圖時
LinearLayout ll = = (LinearLayout) findViewById(R.id.Row_id);
ll.removeAllViews();
View element = LayoutInflater.from(this).inflate(R.layout.iv, ll, false);
ImageView image_iv = (ImageView) element.findViewById(R.id.ri_iv);
ll.addView(element);
是的,你可以使用一個警告對話框。如果你想定製對話框使用此 - 首先創建一個XML爲您的對話框
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="@string/app_name" />
<EditText
android:id="@+id/username"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
,並重寫此方法
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
更多信息上Devlopper android
我使用設置可見性的方法。但是你可以等待更多專家的答案,如果還有其他更好的方法,我也會有興趣知道它。 – Arshad
你正在談論通過點擊按鈕打開一個對話框?爲什麼不使用AlertDialog? – Opiatefuchs