亲爱的我正在尝试创建我的SimpleCalendarView 通过以线性布局水平添加7个TextView,然后将其重复7次。 但是我面对的问题是电视的尺寸会根据其内容而变化 ..您能否以编程方式帮助我为每台电视设置合适的尺寸 还是有最好的逻辑可循? 谢谢
我的代码
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = findViewById(R.id.calendar_layout);
int widthPx = getScreenWidthPx();
int paddingVal = widthPx/21;
int paddingVal2 = (int) (widthPx/16.5);
int paddingVal3 = widthPx/17;
String weekDays= "M,T,W,T,F,S,S";
ArrayList<String> weekDaysList = new ArrayList<>(Arrays.asList(weekDays.split(",")));
int dayNumber = 1, newMonthDayNumber = 1; ;
// set days row in calendar view
View calendarDaysRow = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.calendar_row, null, false);
LinearLayout linearLayoutDaysRow = calendarDaysRow.findViewById(R.id.calendar_row);
for (int i = 1; i <7 ; i++) {
View dayItem = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.day_layout, null, false);
TextView dayTV = dayItem.findViewById(R.id.day_tv);
dayTV.setText(weekDaysList.get(i));
dayTV.setPadding(paddingVal3,paddingVal,paddingVal3,paddingVal);
linearLayoutDaysRow.addView(dayTV);
}
linearLayout.addView(linearLayoutDaysRow);
// set month days in calendar view
for (int j = 0; j < 6; j++) {
View calendarRow = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.calendar_row, null, false);
LinearLayout linearLayoutRow = calendarRow.findViewById(R.id.calendar_row);
for (int i = 1; i <7 ; i++) {
View dayItem = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.day_layout, null, false);
final TextView dayTV = dayItem.findViewById(R.id.day_tv);
if (dayNumber < 10) {
dayTV.setText(String.valueOf(dayNumber++));
dayTV.setPadding(paddingVal2,paddingVal,paddingVal2,paddingVal);
}else if (dayNumber > 31){
dayTV.setText(String.valueOf(newMonthDayNumber++));
dayTV.setPadding(paddingVal2,paddingVal,paddingVal2,paddingVal);
dayTV.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
}else {
dayTV.setText(String.valueOf(dayNumber++));
dayTV.setPadding(paddingVal,paddingVal,paddingVal,paddingVal);
}
dayTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = dayTV.getText().toString();
Toast.makeText(getBaseContext(),text,Toast.LENGTH_SHORT).show();
}
});
linearLayoutRow.addView(dayTV);
}
linearLayout.addView(linearLayoutRow);
}
}
private int getScreenWidthPx (){
DisplayMetrics metrics = getResources().getDisplayMetrics();
return metrics.widthPixels;
}
}
day.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@color/colorAccent"
android:text="20"
android:maxLines="1"
android:id="@+id/day_tv"
android:background="@drawable/day_back_ground"/>
row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/calendar_row"/>
答案 0 :(得分:1)
您可以使用textview属性“ ems”,例如
android:ems
设置TextView的宽度以适合n个“ M”个字母的文本,而不考虑实际的文本扩展名和文本大小。
答案 1 :(得分:0)
答案 2 :(得分:0)
您可以使用此功能处理所有dp和sp:
实施'com.intuit.sdp:sdp-android:1.0.5'
并像这样在xml中使用:-android:padding =“ @ dimen / _10sdp”也在R.dimen._100sdp这样的java / kotlin文件中。
并在程序上使用它:-txtView.setTextSize(R.dimen._18sdp);