2017-08-28 25 views
0

我正在處理自定義視圖,它應該具有相同的高度和寬度。調整視圖的大小到給定的空間

我繪製它以下述方式:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    canvas.drawCircle(getWidth()/2, getHeight()/2, getWidth()/2, mPaint); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    widthMeasureSpec = heightMeasureSpec; 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

如果我僅添加一個視圖到屏幕上,寬度/高度是確定。但是,如果我將這些視圖中的兩個相鄰添加,則應調整視圖的大小(每個視圖:屏幕的半寬)。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <at.guger.widget.Light 
     android:id="@+id/lights_light1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_margin="5dp" /> 

    <at.guger.widget.Light 
     android:id="@+id/lights_light2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_margin="5dp" /> 

</LinearLayout> 

我該怎麼做?

回答

0

讓視圖佔用儘可能多的空間不是更容易,但將它們限制在父級佈局管理器之下嗎?您可以使用recyclerview和自己的LayoutManager實現它,並重載measureChild方法,以便返回widthscreen除以適配器中elememnt的數量。不工作的例子,但一些沿線:

// setting up your recycler view 
mRecyclerView = (RecyclerView) mView.findViewById(R.id.recycler); 

// attaching your custom adapter 
mRecyclerAdapter = new MyRecyclerAdapter(getActivity(), this); 
mRecyclerView.setAdapter(statusRecyclerAdapter); 

// setting the layoutmanager 
mRecyclerLayoutManager = new MyRecyclerLayoutManager(
           getActivity(), 
           LinearLayoutManager.HORIZONTAL, false); 
mRecyclerView.setLayoutManager(mRecyclerLayoutManager); 

然後,你將創建一個內部類MyRecyclerLayoutManager如下:

private class MyRecyclerLayoutManager extends LinearLayoutManager { 

     MyRecyclerLayoutManager(Context context, int orientation, boolean reverseLayout) { 
      super(context, orientation, reverseLayout); 
     } 

     public void measureChild(View child, int widthUsed, int heightUsed) { 
      super.measureChild(child, widthUsed, heightUsed); 
      DisplayMetrics displaymetrics = new DisplayMetrics(); 
      getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
      int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels/mRecyclerAdapter.size()), View.MeasureSpec.EXACTLY); 

      final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); 
      final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), 
        getPaddingTop() + getPaddingBottom() + heightUsed, lp.height, 
        canScrollVertically()); 

      child.measure(widthSpec, heightSpec); 
     } 

     public void measureChildWithMargins(View child, int widthUsed, int heightUsed) { 
      super.measureChildWithMargins(child, widthUsed, heightUsed); 
      DisplayMetrics displaymetrics = new DisplayMetrics(); 
      getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); 
      int widthSpec = View.MeasureSpec.makeMeasureSpec((int) Math.round(displaymetrics.widthPixels * 0.85), View.MeasureSpec.EXACTLY); 

      final RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) child.getLayoutParams(); 
      final int heightSpec = getChildMeasureSpec(getHeight(), getHeightMode(), 
        getPaddingTop() + getPaddingBottom() + heightUsed, lp.height, 
        canScrollVertically()); 

      child.measure(widthSpec, heightSpec); 
     } 
    } 

在這裏,重要的一點是在widthSpec是窗口寬度由您recycleradapter大小劃分。