如何以编程方式将固定大小设置为 TextView?

How to set fixed size to TextView programmatically?

亲爱的,我正在尝试创建我的 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"/>

您需要一个有助于支持多个屏幕的可缩放尺寸单元。

  • sdpdpdip 的替代品(针对浏览量)
  • sspsp 的替代品(用于文本)

您可以使用此依赖项来处理所有 dp 和 sp : 实施'com.intuit.sdp:sdp-android:1.0.5' 并像这样在 xml 中使用:- android:padding="@dimen/_10sdp" 也在 java/ kotlin 文件中像这样 R.dimen._100sdp.
并以编程方式使用它:- txtView.setTextSize(R.dimen._18sdp);

你可以使用 textview 属性 "ems" 比如 android:ems

设置 TextView 的宽度以适合 n 'M' 个字母的文本,而不管实际的文本扩展名和文本大小。