2016-05-13 153 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乾杯!謝謝 :)。 –