2016-05-13 119 views
0

我使用的是布局充气器而不是菜单充气器,其中布局在页面中心膨胀而不是在右侧显示。 我曾尝试将重力改为正确,但没有奏效。如何将布局充气器对齐

这里是我的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(); 

} 

截图: ​​

+0

也许你需要调整你提示你的视图的'Alert Dialog'。 –

+0

如何做到这一点,你可以告诉我 –

+0

你想在中间展示什么? – AbhayBohra

回答

2

你需要设置你的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 DialogGravity按您选择。

+0

对不起,我已经检查出错了 –

+0

@GnanaPrakash干杯!谢谢 :)。 –