2012-07-27 54 views
2

在我的某些活动中,我使用的主题 -安卓:获得活动窗口的尺寸

机器人:主题= “@安卓风格/ Theme.Dialog”

有什么办法如何找出屏幕上这个窗口的实际尺寸?我最后的努力结束于:

Display display = getWindowManager().getDefaultDisplay(); 
Point size = new Point(); 

这是给我整个屏幕的实际分辨率。有任何想法吗?

THX

回答

0

不知道这是完全正确的解决方案,但我通过获取从布局会人为增加你的对话框根视图解决了这个问题(您需要分配在这种情况下,ID)和它之后相当直接获得尺寸:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/root" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="right" 
    android:layout_marginTop="103dp" 
    android:background="#FFFFFF"> 

    <RelativeLayout 
     android:id="@+id/report_header" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:paddingLeft="15dp"> 
     .... 
    </RelativeLayout> 
</RelativeLayout> 

在你的对话活动中,你需要得到你的根:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.dialog_layout); 
    View root = findViewById(R.id.root); 
} 

之后,你可以得到对话框d通过imensions:

Rect r = new Rect (0, 0, 0, 0); 
root.getHitRect (r); 

这个电话后,r将包含你正在寻找

+1

谢谢,但不工作。我所做的与您的建议相同。 R.width()给我0 – Waypoint 2012-08-01 13:45:38

-1

尺寸有两种方法,你可以这样做:第一个是这样的:

  Display display = getActivity().getWindowManager().getDefaultDisplay(); 
      int width = display.getWidth(); 
      int widthdp = (int) (width/getResources().getDisplayMetrics().density); 
      int height = display.getHeight(); 
      int heightdp = (int) (height/getResources().getDisplayMetrics().density); 

第二个你可以使用是这样的:

   Point size = new Point(); 
       display.getSize(size); 
       int width_one = size.x; 
       int height_one = size.y; 
+0

感谢您的回复,但这不起作用。无法创建点解决方案(display.getSize(size)不存在,它需要投射,这是不工作的)。第一个是给我不好的决议。 – Waypoint 2012-08-01 13:38:29

+0

嘿,而不是做(宽度/ getResoruces().....)尝试做(宽* getResources()....) – Android2390 2012-08-01 16:55:04

+0

也是什么是你试图找到维度的看法?它是一个片段还是一个webview或只是一个线性/相对布局? – Android2390 2012-08-01 16:56:02

0

有一个很简单的方法

<RelativeLayout mlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rootView" 
     > 
... some other views 
    </RelativeLayout> 

比,在你的活动,你需要得到的观点,一旦它的绘制。

这样的:

private View mRoot; 

    @Override 
    protected void onResume() { 
     super.onResume(); 

     mRoot = findViewById(R.id.rootView); 

     /* after the main view is loaded, start positioning the views */ 
     mRoot.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
      @Override 
      public void onGlobalLayout() { 
// in here, the view is already loaded, you can get his size. 
// without the navigation/status/title bar 
       // remove the observer, we don't need it anymore 
mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this); 
       mButton.setRadius(1000); 
       mButton.setMinimumWidth(mRoot.getWidth()/2); 
       mButton.setY(mRoot.getHeight()/3 * 2); 
      } 
     }); 
    } 

希望帮助。