2013-04-07 104 views
0

我想創建適用於每種類型平板電腦的以下佈局。我知道我們可以通過將它們放置在可繪製和編輯它們來設置不同的設置,但是有沒有其他方法可以實現這一點? 以下是我的本意爲7寸大小的所有藥片進行創建: enter image description here優化alertDialog佈局?

下面是我的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" 
    > 
<TextView 
    android:id="@+id/widget38" 
    android:layout_width="480dp" 
    android:layout_height="45dp" 
    android:background="@color/White" 
    android:text="Reminder" 
    android:textSize="20dp" 
    android:textColor="@color/Black" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" /> 

<TextView 
    android:id="@+id/label" 
    android:layout_width="476dp" 
    android:layout_height="70dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/widget38" 
    android:background="@color/White" 
    android:text="DID you have your Pill?" 
    android:textColor="@color/Black" 
    android:textSize="40dp" /> 

<Button 
    android:id="@+id/yes" 
    android:layout_width="224dp" 
    android:layout_height="161dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignRight="@+id/widget38" 
    android:layout_below="@+id/label" 
    android:background="@color/Green" 
    android:text="Yes" /> 

<Button 
    android:id="@+id/no" 
    android:layout_width="239dp" 
    android:layout_height="161dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/label" 
    android:layout_toRightOf="@+id/widget38" 
    android:background="@color/Red" 
    android:text="No/Cancel" /> 

</RelativeLayout> 

我的Java代碼,創建警報:

Button button=(Button)findViewById(R.id.button1); 
     button.setOnClickListener(new View.OnClickListener() { 

      @SuppressWarnings("deprecation") 
      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 


       View view= getLayoutInflater().inflate(R.layout.custom, null); 
       final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 


       builder.setView(view); 
       // set the custom dialog components - text, image and button 
        TextView t=(TextView)view.findViewById(R.id.widget38); 
       TextView text = (TextView)view.findViewById(R.id.label); 

       Button yesButton = (Button) view.findViewById(R.id.yes); 
       Button noButton=(Button)view.findViewById(R.id.no); 



       final AlertDialog dialog=builder.create(); 

       noButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 



       yesButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         dialog.dismiss(); 
        } 
       }); 

      Display display = getWindowManager().getDefaultDisplay(); 
       int width = display.getWidth(); 
       int height = display.getHeight(); 

       dialog.show(); 
       dialog.getWindow().setLayout(width, height); 

根據答案編輯代碼,並且它在eclipse IDE中正確顯示,但是在模擬器上嘗試時看起來非常不同。無法recitify..Please幫助。 output that appears in nexus 7 emulator however it shows correctly in eclipse xml editor

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     > 

     <TextView 
      android:id="@+id/ReminderMessage" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:background="@color/Aqua" 
     android:textColor="@color/Black" 
    android:textSize="10dp" 
      /> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" 
     android:baselineAligned="false" 
     android:layout_alignParentLeft="true" 
    android:layout_alignParentRight="true" 

     > 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#0F0" > 

      <Button 
       android:id="@+id/yes" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="Yes" 
      android:background="@color/Green" 
      android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/ReminderMessage" 


       > 
      </Button> 



     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#F00" > 


      <Button 
       android:id="@+id/no" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="No/Cancel" 
      android:background="@color/Red" 
      android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_below="@+id/ReminderMessage" 
       > 
      </Button> 

     </RelativeLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

我的錯,我忘了渲染時的線性佈局中使用反向權重:http://stackoverflow.com/questions/8381021/why-does-the-linearlayout-attribute-layout-weight-seem-to - 與w相反。只需切換頂部/底部部分的權重,它應該按照inteneded工作 – munch1324 2013-04-07 19:48:24

+0

啊現在可以工作了...它在Eclipse中顯示的不同,但它顯示了設備中的預期方式..這是有目的地通過谷歌或WA錯誤?..再次感謝很多.... – 2013-04-08 10:03:30

+0

我不確定是誠實的。我知道它也欺騙了我幾次:-) – munch1324 2013-04-08 11:26:49

回答

1

編輯:修改爲示出了用於線性佈局逆加權。

如果你想有一個不同的外觀不同尺寸,佈局/佈局大/佈局XLARGE文件夾將是定義警報特異於不同尺寸範圍(例如7" )的最簡單的方法。

如果您使用Eclipse作爲您的IDE,請選擇「所有屏幕」,以便對各種設備進行良好的概述。

除此之外,如果您有特定的比率,線性佈局是一種簡單的方法來定義這裏是你的線性佈局(它將保持屏幕尺寸的比例):

enter image description here

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="3" > 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" > 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#0F0" > 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:background="#F00" > 

     </RelativeLayout> 

    </LinearLayout> 

</LinearLayout> 
+0

噢好吧,不知道有一個預覽屏幕選項在adt ....感謝噸! – 2013-04-07 16:38:41

+0

根據您的答案進行了編輯,但它並未在設備上正確顯示,儘管它在eclipse IDE中正確顯示。細節在上面編輯。 – 2013-04-07 19:25:05