繁体   English   中英

如何将布局充气机向右对齐

[英]How to align the layout inflater to the right

我使用的是布局填充器,而不是菜单填充器,在该菜单中,布局在页面的中央被填充,而不是在右上角显示。 我曾尝试将重力更改为正确的值,但没有用。

这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="150dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:background="@color/primary"
android:orientation="vertical"
android:layout_alignParentEnd="true">
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:id="@+id/linshare"
    android:paddingTop="5dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:background="@drawable/share"
        android:layout_height="wrap_content" />
     <TextView
         android:layout_width="match_parent"
         android:text="Share"
         android:textSize="16sp"
         android:gravity="center"
         android:layout_gravity="center_horizontal"
         android:textColor="#FFFFFF"
         android:layout_height="match_parent" />
    </LinearLayout>
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:paddingTop="5dp"
    android:id="@+id/lininvite"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:background="@drawable/invite"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="match_parent"
        android:text="Invite Friends"
        android:textSize="16sp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:layout_height="match_parent" />
 </LinearLayout>
 <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF"/>
 </LinearLayout>
 <LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:id="@+id/linrate"
    android:paddingTop="5dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:background="@drawable/rate"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="match_parent"
        android:text="Rateus"
        android:textSize="16sp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:layout_height="match_parent" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF"/>
  </LinearLayout>
  <LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:paddingTop="5dp"
    android:id="@+id/linhelp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:background="@drawable/help"
        android:layout_height="wrap_content" />
    <TextView
        android:layout_width="match_parent"
        android:text="Help"
        android:textSize="16sp"
        android:gravity="center"
        android:layout_gravity="center_horizontal"
        android:textColor="#FFFFFF"
        android:layout_height="match_parent" />
  </LinearLayout>
  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="10dp">
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#FFFFFF"/>
  </LinearLayout>
  </LinearLayout>
   </RelativeLayout>

码:

private void menz() {
    LayoutInflater layoutInflater = LayoutInflater.from(Additionone.this);
    View promptView = layoutInflater.inflate(R.layout.menuinflate, null);
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Additionone.this);
    alertDialogBuilder.setView(promptView);
    final LinearLayout share = (LinearLayout) promptView.findViewById(R.id.linshare);
    final LinearLayout invite = (LinearLayout) promptView.findViewById(R.id.lininvite);
    final LinearLayout rate = (LinearLayout) promptView.findViewById(R.id.linrate);
    final LinearLayout help = (LinearLayout) promptView.findViewById(R.id.linhelp);
    share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "Future of Learning http://www.pencilruler.com/ ");
            sendIntent.setType("text/plain");
            startActivity(sendIntent);

        }
    });
    invite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL, new String[]{""});
            i.putExtra(Intent.EXTRA_SUBJECT, "QUIZ APP");
            i.putExtra(Intent.EXTRA_TEXT, "You have to try this quiz app");
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));
            } catch (android.content.ActivityNotFoundException ex) {
                Snackbar snackbar = Snackbar
                        .make(coordinatorLayout, "NO EMAIL CLIENTS INSTALLED", Snackbar.LENGTH_LONG);

                snackbar.show();

            }
        }
    });
    help.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(Additionone.this,HelpAct.class);
            startActivity(i);
            Additionone.this.finish();

        }
    });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();

}

屏幕截图: 在此处输入图片说明

您需要在其中提示视图时设置“ Alert Dialog Builder Alignment

使用setGravity(Gravity.TOP | Gravity.RIGHT);

请参考此。

Window window = alertDialogBuilder.getWindow();
window.setLayout(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.TOP | Gravity.RIGHT);

注意:您可以根据需要更改“ Alert Dialog Gravity ”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM