2013-04-23 43 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)));