我正在努力使高度与gridview项目的宽度匹配。我实现了我的项目填充了列宽,并且列高度与项目宽度匹配,但是项目高度不适应。
Gridview项的类:
public class Day extends LinearLayout {
public String date;
public int dayOfMonth;
public int dayOfWeek;
private TextView viewDayOfMonth;
public Day(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
Init();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, widthMeasureSpec);
}
private void Init(){
inflate(getContext(), R.layout.day_layout, this);
viewDayOfMonth = findViewById(R.id.day_ofMonth);
}
public void Build(int _day){
dayOfMonth = _day;
viewDayOfMonth.setText(String.valueOf(dayOfMonth));
}
}
布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#424242"
android:gravity="center"
android:layout_margin="1dp">
<TextView
android:id="@+id/day_ofMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="17dp"
android:text="15"/></LinearLayout>
适配器
public class ObschtCalenderAdapter extends BaseAdapter {
private Day[] days;
private Context context;
public ObschtCalenderAdapter(Context context){
this.context = context;
this.days = new Day[14];
for(int i = 0; i < days.length; i++) {
days[i] = new Day(context, null);
}
}
@Override
public int getCount() {
return days.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout view = days[position];
days[position].Build(position);
return days[position];
}
}
有帮助吗?谢谢。