Android 如何将imageview放在自定义对话框的中心顶部?
我正在创建一个自定义对话框,我想在其中将Android 如何将imageview放在自定义对话框的中心顶部?,android,android-layout,android-alertdialog,Android,Android Layout,Android Alertdialog,我正在创建一个自定义对话框,我想在其中将ImageView放在该对话框的中心顶部 在相对布局框中设置此imageview,并为imageview标记设置这两个属性 android:layout_centerHorizontal="true" android:layout_marginTop="-50dp" 在相对布局框中设置此imageview,并为imageview标记设置这两个属性 android:layout_centerHorizontal="true" android
ImageView
放在该对话框的中心顶部
在相对布局框中设置此imageview,并为imageview标记设置这两个属性
android:layout_centerHorizontal="true"
android:layout_marginTop="-50dp"
在相对布局框中设置此imageview,并为imageview标记设置这两个属性
android:layout_centerHorizontal="true"
android:layout_marginTop="-50dp"
根据您的想法,您可以将您的
视图
,放入线性布局
对话方法:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mhp);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
mhp.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:background="#f00"
android:orientation="vertical" >
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
结果(避免BG)
您可以根据自己的想法将您的
视图
放在线性布局内
对话方法:
Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mhp);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
mhp.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:background="#f00"
android:orientation="vertical" >
</LinearLayout>
<ImageView
android:id="@+id/imageView1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
结果(避免BG)
试试这个
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="90dp"
android:background="@color/colorPrimary"
>
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher"
/>
</FrameLayout>
试试这个
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_marginTop="90dp"
android:background="@color/colorPrimary"
>
</LinearLayout>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
android:src="@mipmap/ic_launcher"
/>
</FrameLayout>
public void isAppUpdateAvailable(){
LayoutInflater LayoutInflater=LayoutInflater.from(getActivity());
视图=布局更平坦。充气(R.layout.update\U available\U对话框,空);
final AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setView(视图);
final AlertDialog AlertDialog=builder.create();
alertDialog.requestWindowFeature(窗口.功能\u无\u标题);
alertDialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
text查看btn确认,btn_取消;
btn\u cancel=view.findviewbyd(R.id.btn\u cancel);
btnConfirm=view.findviewbyd(R.id.btn\u确认);
btn_cancel.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
alertDialog.disclose();
}
});
btnConfirm.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
alertDialog.disclose();
}
});
alertDialog.show();
}
**white_transparent_k.xml**
**radius_top.xml**
**radius_bottom.xml**
public void isAppUpdateAvailable(){
LayoutInflater LayoutInflater=LayoutInflater.from(getActivity());
视图=布局更平坦。充气(R.layout.update\U available\U对话框,空);
final AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
builder.setView(视图);
final AlertDialog AlertDialog=builder.create();
alertDialog.requestWindowFeature(窗口.功能\u无\u标题);
alertDialog.getWindow().setBackgroundDrawable(新的ColorDrawable(Color.TRANSPARENT));
text查看btn确认,btn_取消;
btn\u cancel=view.findviewbyd(R.id.btn\u cancel);
btnConfirm=view.findviewbyd(R.id.btn\u确认);
btn_cancel.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
alertDialog.disclose();
}
});
btnConfirm.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
alertDialog.disclose();
}
});
alertDialog.show();
}
**white_transparent_k.xml**
**radius_top.xml**
**radius_bottom.xml**
您可以使用FrameLayout进行相同的操作,1。制作第一个元素,即橙色线框,从顶部留出一些边距。2.第二个创建带重心的图像视图,framelayout的第二个视图,将显示在第一个布局的顶部。希望对您有所帮助。您可以使用FrameLayout进行相同的操作,1。制作第一个元素,即橙色线框,从顶部留出一些边距。2.第二个创建带重心的图像视图,framelayout的第二个视图,将显示在第一个布局的顶部。希望能有帮助。哪些属性?哪些属性?