如何在android中单击按钮时显示View

时间:2016-01-24 16:01:33

标签: java android relativelayout onclicklistener

怎么可能?谢谢!我应该将我的View(RelativeLayout)置于另一个View的顶部并使其不可见,当用户单击该按钮时,它会将其设置为可见吗?或者是否有另一种更简单的方法可以在屏幕中间打开对话框(?)?

谢谢!

2 个答案:

答案 0 :(得分:0)

您需要一个布局来放置子视图。最安全的是线性布局,因为添加多个孩子很容易

Fisrt在布局中创建一个xml文件。

例如布局 添加名为say 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);

答案 1 :(得分:0)

是的,您可以使用警报对话框进行设置。如果要自定义对话框,请使用此选项 - 首先为对话框创建一个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

的更多信息
相关问题