2017-08-28 21 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); 
} 

私は画面上にあるビューを追加した場合、幅/高さはokです。ただし、これらのビューの2つを隣り合わせに追加すると、ビューのサイズを調整する必要があります(各ビュー:画面の半分の幅)。

<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); 

その後、あなたは次のようにinnerclass 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); 
     } 
    } 

重要なビットあなたrecycleradapterサイズで割っウィンドウ幅であるwidthSpecであります。

関連する問題