1. Introduce thinking scenarios
Let’s look at the code for getView in the ListView Adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if(view == null || ! (view.getTag() instanceof ViewHolder)) { view = new MyRelativeLayout(mContext); viewHolder = new ViewHolder(view); view.setTag(viewHolder); }else{ viewHolder = (ViewHolder) view.getTag(); } // the following code is ignoredCopy the code
Just to explain MyRelativeLayout is my custom RelativeLayout MyRelativeLayout code
public class MyRelativeLayout extends RelativeLayout { public MyRelativeLayout(Context context) { super(context); LayoutInflater.from(getContext()).inflate(R.layout.list_item, this); } public MyRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); LayoutInflater.from(getContext()).inflate(R.layout.list_item, this); } // the following code is ignoredCopy the code
R.layout.list_item XML contains the following contents
<? xml version="1.0" encoding="utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:id="@+id/tx"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:gravity="center"
android:text="@string/app_name"
android:textSize="40dp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@id/tx"
android:background="@color/colorPrimary"
/>
</RelativeLayout>
Copy the code
It’s a simple layout and that’s what it looks like
2. Identify problems and think about them
Okay, so the actors are in place, so I’m going to say what are the LayoutParams within MyRelativeLayout, or let’s just say what is the width and height of MyRelativeLayout?
List_item XML is the width and height of the root node of r.layout.list_item XML. Turns out it wasn’t. See the phenomenon
Finally found MyRelativeLayout LayoutParams is AbsListView generateDefaultLayoutParams methods defined in the class
@Override
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
Copy the code
That is, the true width of MyRelativeLayout is what fills the parent form, and the height is the content of the wrap, not the red part of the image above. If a view is new When you are in the add into its parent viewgroup forget set LayoutParams, you of the LayoutParams of the view is by the parent class generateDefaultLayoutParams completion. The ListView is the default completion width of MATCH_PARENT and height of WRAP_CONTENT, not the default completion width of other subclasses of ViewGroup. So take a quick look at the default completion of each ViewGroup and its descendants. Knowing your enemy is the only way to win a hundred battles.
3. Summary
The ViewGroup class is a quick list of common layouts
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
Copy the code
FrameLayout class
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
Copy the code
RelativeLayout class
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
Copy the code
LinearLayout class
protected LayoutParams generateDefaultLayoutParams() {
if (mOrientation == HORIZONTAL) {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
} else if (mOrientation == VERTICAL) {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
return null;
}
Copy the code
AbsListView (i.e., the GridView and the ListView’s father, the GridView and the ListView generateDefaultLayoutParams is AbsListView dad)
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0);
}
Copy the code
ViewPager class
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
public LayoutParams() {
super(MATCH_PARENT, MATCH_PARENT);
}
Copy the code
AbsoluteLayout layout
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0);
}
Copy the code
GridLayout
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
public LayoutParams() {
this(Spec.UNDEFINED, Spec.UNDEFINED);
}
Copy the code
AbsSpinner (Gallery and Spinner two sons and dad are the same)
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
Copy the code
RedioGroup
@Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
Copy the code
TableLayout
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
public LayoutParams() {
super(MATCH_PARENT, WRAP_CONTENT);
}
Copy the code
SlidingPaneLayout
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams();
}
public LayoutParams() {
super(MATCH_PARENT, MATCH_PARENT);
}
Copy the code
ConstraintLayout (-2 WRAP_CONTENT)
protected ConstraintLayout.LayoutParams generateDefaultLayoutParams() {
return new ConstraintLayout.LayoutParams(-2, -2);
}
Copy the code
DrawerLayout
protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
Copy the code
Well, should cover the mainstream ViewGroup control, as for how to remember, in fact, check the source code is good, I listed here is for later access to a convenient figure.