2016-11-06 6 views
0

Androidがどのようにビューを測定し、レイアウトするかを理解しようとしています。カスタムビューとビューグループを持つサンプルアプリケーションを作成しました。onMeasure onMeasureとonLayoutの子ビューは、親のrequestLayoutの後に呼び出されません。

CustomViewGroup

public class CustomViewGroup extends ViewGroup { 

    private static final String LOG_TAG = "CustomViewGroup"; 

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     Log.d(LOG_TAG, String.format(Locale.ENGLISH, 
       getTag() + " " + 
         "onMeasure(widthMeasureSpec=%d, heightMeasureSpec%d)", 
       widthMeasureSpec, heightMeasureSpec 
     )); 

     final int count = getChildCount(); 
     for (int i = 0; i < count; i++) { 
      final View child = getChildAt(i); 
      if (child.getVisibility() != View.GONE) { 
       // Make or work out measurements for children here (MeasureSpec.make...) 
       measureChild(child, widthMeasureSpec, heightMeasureSpec); 
      } 
     } 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     Log.d(LOG_TAG, String.format(Locale.ENGLISH, 
       getTag() + " " + 
         "onLayout(changed=%b, left=%d, top=%d, right=%d, bottom=%d)", 
       changed, left, top, top, right, bottom 
     )); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Log.d(LOG_TAG, getTag() + " " + "onDraw"); 
    } 
} 

のCustomView

public class CustomView extends View { 

    private static final String LOG_TAG = "CustomView"; 

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

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     Log.d(LOG_TAG, String.format(Locale.ENGLISH, 
       getTag() + " " + 
       "onLayout(changed=%b, left=%d, top=%d, right=%d, bottom=%d)", 
       changed, left, top, top, right, bottom 
     )); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     Log.d(LOG_TAG, getTag() + " " +"onDraw"); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     Log.d(LOG_TAG, String.format(Locale.ENGLISH, 
       getTag() + " " + 
       "onMeasure(widthMeasureSpec=%d, heightMeasureSpec=%d)", 
       widthMeasureSpec, heightMeasureSpec 
     )); 
    } 
} 

MainActivity

public class MainActivity extends AppCompatActivity { 

    private View mCustomGroup1; 
    private View mCustomGroup2; 
    private View mContainer; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Log.d("MainAct", "onCreate"); 
     mCustomGroup1 = findViewById(R.id.requestLayout); 
     mCustomGroup2 = findViewById(R.id.requestLayout2); 
     mContainer = findViewById(R.id.activity_main); 

     findViewById(R.id.requestLayoutBtn).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mCustomGroup1.requestLayout(); 
      } 
     }); 

     findViewById(R.id.requestLayoutBtn2).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mCustomGroup2.requestLayout(); 
      } 
     }); 

     findViewById(R.id.requestLayoutContBtn).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mContainer.requestLayout(); 
      } 
     }); 

    } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="ru.dmitriev.squareorderedlayout.MainActivity"> 

    <Button 
     android:id="@+id/requestLayoutBtn" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="requestLayout" /> 

    <Button 
     android:id="@+id/requestLayoutBtn2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="requestLayout2" /> 

    <Button 
     android:id="@+id/requestLayoutContBtn" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="requestLayoutCont" /> 

    <ru.dmitriev.squareorderedlayout.CustomViewGroup 
     android:id="@+id/requestLayout" 
     android:layout_width="100dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#7f7" 
     android:tag="CustomVieGroup1"> 

     <ru.dmitriev.squareorderedlayout.CustomView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="CustomView11" /> 

     <ru.dmitriev.squareorderedlayout.CustomView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="CustomView12" /> 
    </ru.dmitriev.squareorderedlayout.CustomViewGroup> 

    <ru.dmitriev.squareorderedlayout.CustomViewGroup 
     android:id="@+id/requestLayout2" 
     android:layout_width="100dp" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#f77" 
     android:tag="CustomVieGroup2"> 

     <ru.dmitriev.squareorderedlayout.CustomView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="CustomView21" /> 

     <ru.dmitriev.squareorderedlayout.CustomView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:tag="CustomView22" /> 

    </ru.dmitriev.squareorderedlayout.CustomViewGroup> 
</LinearLayout> 

活動が始まり、onMeasureonLayoutonDrawがビュー・グループで呼び出されactivity_main.xmlはCustomVieGroup1CustomVieGroup2タグ付き。各ビューグループは、子プロセスでこれらのメソッドを呼び出します。大丈夫です。わかっている。

私はビュー・グループにrequestLayoutを呼び出すCustomVieGroup 、onMeasureonLayoutをタグ付けしてonDrawはCustomVieGroup に呼ばれています。グループは、これらのメソッドを子プロセスで呼び出します。わかっている。

私はビュー・グループにrequestLayoutを呼び出すCustomVieGroup 、onMeasureonLayoutをタグ付けしてonDrawはCustomVieGroup に呼ばれています。グループは、これらのメソッドを子プロセスで呼び出します。私もそれを理解しています。

しかし、私はID activity_mainonMeasureonLayoutCustomVieGroup1またはCustomVieGroup2で呼び出されていないとの見解にrequestLayoutを呼び出すとき。どうして?私はdocumentation of requestLayoutによればonMeasureonLayoutonDraw

両方CustomVieGroup1CustomVieGroup2(ID activity_main有するビューのすなわちcgildren)で呼び出されることが期待:

これのレイアウトパスをスケジュールしますビューツリー。

答えて

4

requestLayout()LinearLayout(@ ID/activity_main)のonLayout()方法を実行するようになります。ただし、LinearLayoutがその子を再レイアウトするという保証はありません。それは実際にその実装に依存します。たとえば、LinearLayoutの幅/高さはmatch_parentに設定されています。 LinearLayoutが2回目にレイアウトされているときは、そのサイズは子のサイズではなく親のサイズに基づいているため、幅と高さは変更されません。そのレイアウトはその子に基づいていないので、LinearLayoutはその子を再レイアウトする必要はありません。そのため、サイズが親に一致するほぼすべてのViewGroupのレイアウトを無効にしても、そのレイアウトの子がレイアウトされることはありません。

LinearLayout(@ id/activity_main)を 'wrap_content'に変更していることを確認する必要があります。これにより、レイアウトのサイズはその子に依存するようになります。そのため、レイアウトを変更する必要があります。これにより、カスタムビューレイアウトメソッドが呼び出されます。希望があれば、

+0

絶対に正しい! 'match_parent'を' wrap_content'に変更することで、 –

関連する問題