标签:
public class DensityUtils {
/**
* dp转px
*/
public static int dp2px(Context ctx, float dp) {
float density = ctx.getResources().getDisplayMetrics().density;//获取屏幕像素密度
int px = (int) (dp * density + 0.5f);// 4.9->5 4.4->4
return px;
}
public static float px2dp(Context ctx, int px) {
float density = ctx.getResources().getDisplayMetrics().density;
float dp = px / density;
return dp;
}
}
<View
android:id="@+id/view_red_point"
android:layout_width="10dp"
android:layout_height="10dp"
android:background="@drawable/shape_point_red" />
// 初始化引导页的小圆点
for (int i = 0; i < mImageIds.length; i++) {
View point = new View(this);
point.setBackgroundResource(R.drawable.shape_point_gray);// 设置引导页默认圆点
- //这里面是像素dx,转换成dp
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
DensityUtils.dp2px(this, 10), DensityUtils.dp2px(this, 10));
if (i > 0) {
params.leftMargin = DensityUtils.dp2px(this, 10);// 设置圆点间隔
}
point.setLayoutParams(params);// 设置圆点的大小
llPointGroup.addView(point);// 将圆点添加给线性布局
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
setBehindContentView(R.layout.left_menu);// 设置侧边栏
SlidingMenu slidingMenu = getSlidingMenu();// 获取侧边栏对象
slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);// 设置全屏触摸
int width = getWindowManager().getDefaultDisplay().getWidth();// 获取屏幕宽度
slidingMenu.setBehindOffset(width * 200 / 320);// 设置预留屏幕的宽度,按比例
initFragment();
}
标签:
原文地址:http://www.cnblogs.com/liuyu0529/p/4919352.html