2013-04-23 81 views
0

如何在Android中創建對話框(基於表格佈局),寬度與屏幕寬度成比例(例如,60%)?以下代碼將佔用屏幕的所有寬度。Android:按比例寬度的對話框

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
     <TableRow 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <ImageView 
        android:id="@+id/image" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_margin="5dp"/> 
      <TextView 
        android:id="@+id/text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:textColor="#FFF" 
        android:layout_margin="5dp"/> 
     </TableRow> 
     <TableRow 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent"> 
      <Button 
        android:id="@+id/dialogButtonOK" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Ok" 
        android:gravity="center" 
        android:layout_below="@+id/text" 
        android:layout_span="2"/> 
     </TableRow> 
    </TableLayout> 
+0

張貼了它現在看起來如何的截圖。通常對話框不佔用屏幕寬度的100%,如果你的是我們需要看看你是如何創建和顯示它。 – FoamyGuy 2013-04-23 20:05:08

回答

0

據我所知,這是不可能的,但也許這可能工作;在您的活動試圖訪問你的XML佈局 - TableLayout TL =(TableLayout)findViewById(...),並試圖讓你的設備屏幕寬度

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

,然後通過

設置基於該寬度一定的價值
rl.setLayoutParams(new LayoutParams(width*0.7,LayoutParams.WRAP_CONTENT(or something else)));