您的位置:首页 > 移动开发 > Android开发

Android学习记录(十五) --界面随谈。

2016-05-20 20:05 357 查看
最近在重新改ui界面,随便记录点东西。

1.控件的高度自定义。

//获取屏幕属性
WindowManager wm = (WindowManager) this
.getSystemService(Context.WINDOW_SERVICE);
int width = wm.getDefaultDisplay().getWidth() / 2;// 屏幕宽度

ImageView picMovieAll = (ImageView) findViewById(R.id.pic_movie_all);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) picMovieAll.getLayoutParams();
linearParams.height = width;
picMovieAll.setLayoutParams(linearParams);


2.gridview 高度自适应。

public class UnScrolledGridView extends GridView {

public SquareGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
getLayoutParams().height = getMeasuredHeight();
}
}


3.gridview image item 正方形

public class GridViewItem extends ImageView {

public GridViewItem(Context context) {
super(context);
}

public GridViewItem(Context context, AttributeSet attrs) {
super(context, attrs);
}

public GridViewItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec); // This is the key that will make the height equivalent to its width
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: