2016-11-06 35 views
0

我想了解Android的措施和布局意见。我使用自定义视图和视图组创建了一个示例应用程序。onLineout和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(); 
      } 
     }); 

    } 
} 

activity_main.xml中

<?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被称为在视图组标记CustomVieGroup1CustomVieGroup2。每个视图组都会在子项上调用这些方法。没关系。我明白。

当我调用requestLayout的视图组标记CustomVieGroup ,onMeasureonLayoutonDraw被称为上CustomVieGroup 。该小组将这些方法称为他们的孩子。我明白。

当我调用requestLayout的视图组标记CustomVieGroup ,onMeasureonLayoutonDraw被称为上CustomVieGroup 。该小组将这些方法称为他们的孩子。我也了解它。

但是当我打电话requestLayout上的观点与ID activity_mainonMeasureonLayout不叫上CustomVieGroup1CustomVieGroup2。为什么?我预计,onMeasureonLayoutonDraw将两个CustomVieGroup1CustomVieGroup2

(即视图的ID为activity_main cgildren)被调用按照documentation of requestLayout

这将预定的布局传递视图树。

回答

4

requestLayout()将使LinearLayout(@ ID/activity_main)的onLayout()方法来执行。但是,我们无法保证LinearLayout会重新安排其子女;它真的取决于它的实现。例如,您的宽度/高度LinearLayout设置为match_parent。当第二次铺设LinearLayout时,其宽度和高度不会改变,因为它的大小取决于其父级大小,而不是其子级大小。因为它的布局不是基于它的孩子,所以不需要LinearLayout来重新布置它的孩子。这就是为什么无效几乎任何ViewGroup的布局,其大小匹配其父母不会导致该布局的孩子布局。

您应该能够确认这是将您的LinearLayout(@ id/activity_main)更改为'wrap_content'。通过这样做,布局的大小取决于其子项,因此需要重新布置子项。这将导致您的自定义视图布局方法被调用。希望这会有所帮助,

+0

绝对正确!将'match_parent'改为'wrap_content'会有所不同 –

相关问题