我想获得屏幕宽度,并将其设置为我的布局,这是水平滚动视图的子项。我这样做但它使我的布局比我想要的更宽。
填充是用倾角定义的。
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
int padding = getResources().getDimensionPixelSize(R.dimen.padding);
layoutWidth = (int) ((outMetrics.widthPixels * density) - 2*padding);
答案 0 :(得分:4)
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
float density = getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
float dpWidth = outMetrics.widthPixels / density;
另请参阅:http://developer.android.com/guide/practices/screens_support.html#dips-pels
答案 1 :(得分:2)
您可以使用我的以下代码。
<强> MainActivity.java 强>
package com.mehuljoisar.d_screensize;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onResume() {
super.onResume();
ScreenSizeUtils mScreenSizeUtils = new ScreenSizeUtils(MainActivity.this);
((TextView)findViewById(R.id.tvPx)).setText("wxh (px): "+mScreenSizeUtils.GET_widthxheight_px());
((TextView)findViewById(R.id.tvDp)).setText("wxh (dp): "+mScreenSizeUtils.GET_widthxheight_dp());
}
}
<强> activity_main.xml中强>
<RelativeLayout xmlns: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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tvPx"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tvDp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvPx" />
</RelativeLayout>
<强> ScreenSizeUtils.java 强>
package com.mehuljoisar.d_screensize;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;
public class ScreenSizeUtils {
// helper classes
private Context mContext;
private WindowManager mWm;
private Display mDisplay;
private Configuration mConfig;
private DisplayMetrics mDisplayMetrics, mRealDisplayMetrics;
// helper attributes
private static final int Unknown = -255;
private int width_px, height_px;
private int width_dp, height_dp;
//values
private String widthxheight_px;
private String widthxheight_dp;
public ScreenSizeUtils(Context mContext) {
this.mContext = mContext;
init();
}
private void init() {
mWm = ((WindowManager) mContext
.getSystemService(Context.WINDOW_SERVICE));
mDisplay = mWm.getDefaultDisplay();
mConfig = mContext.getResources().getConfiguration();
mDisplayMetrics = new DisplayMetrics();
mDisplay.getMetrics(mDisplayMetrics);
if(Build.VERSION.SDK_INT >= 17)
{
mRealDisplayMetrics = new DisplayMetrics();
mDisplay.getRealMetrics(mRealDisplayMetrics);
}
}
public String GET_widthxheight_px() {
if (Build.VERSION.SDK_INT < 13) {
width_px = mDisplay.getWidth();
height_px = mDisplay.getHeight();
} else {
Point mPoint = new Point();
mDisplay.getSize(mPoint);
width_px = mPoint.x;
height_px = mPoint.y;
}
widthxheight_px = width_px + " x " + height_px;
return widthxheight_px;
}
public String GET_widthxheight_dp() {
int real_width_px = Unknown;
int real_height_px = Unknown;
if (Build.VERSION.SDK_INT >= 17) {
real_width_px = mRealDisplayMetrics.widthPixels;
real_height_px = mRealDisplayMetrics.heightPixels;
}
else
{
//it is necessary if you haven't called it before
GET_widthxheight_px();
}
height_dp = (int) (((double) ((real_height_px == Unknown) ? height_px
: real_height_px) / mDisplayMetrics.density) + 0.5);
if (Build.VERSION.SDK_INT >= 13) {
width_dp = mConfig.screenWidthDp;
} else {
width_dp = (int) (((double) ((real_width_px == Unknown) ? width_px
: real_width_px) / mDisplayMetrics.density) + 0.5);
}
widthxheight_dp = width_dp + " x " + height_dp;
return widthxheight_dp;
}
}
<强>截图强>
我希望它会有所帮助!!
答案 2 :(得分:0)
此代码将返回宽度(w)&amp;屏幕高度(h)。
DisplayMetrics dMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dMetrics);
float density = dMetrics.density;
int w = Math.round(dMetrics.widthPixels / density);
int h = Math.round(dMetrics.heightPixels / density);
活动是活动的实例,您希望获得屏幕大小。
您必须记住:当您的设备处于横向时,w&gt; H。当它处于纵向时w <小时。
所以从宽度和宽度您可以检测设备的高度是朝哪个方向。