2016-01-24 36 views
0

这怎么可能?谢谢!我应该将View(RelativeLayout)置于另一个视图上并使其不可见,并且当用户单击该按钮时,它将其设置为可见?或者还有另一种在屏幕中间打开对话框(?)的更简单的方法吗?如何在android中点击按钮时出现视图

谢谢!

+1

我使用设置可见性的方法。但是你可以等待更多专家的答案,如果还有其他更好的方法,我也会有兴趣知道它。 – Arshad

+0

你正在谈论通过点击按钮打开一个对话框?为什么不使用AlertDialog? – Opiatefuchs

回答

0

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

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); 
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

相关问题