1. 程式人生 > >Android學習記錄(十五) --介面隨談。

Android學習記錄(十五) --介面隨談。

最近在重新改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 } }