在android中通过移动滑动条调整字体大小

Ane*_*ees -3 android

如何在android中设计屏幕,其中用户可以通过移动滑动条来控制字体大小,如下所示:https: //i.stack.imgur.com/LmHcg.png

Fro*_*and 5

干得好...

  1. 转到您的activity_main.xml。根据您的要求添加 3 个文本视图。添加一个搜索栏。请看下面的代码,以便您能够理解。

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Adjust font size"
        android:textSize="20dp"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="54dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/textView" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Adjust font size by moving slide bar"
        android:textSize="18dp"
        android:layout_marginTop="72dp"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_marginRight="50dp"
        app:layout_constraintRight_toRightOf="parent"
        android:id="@+id/textView2" />
    
    <TextView
        android:id="@+id/changeFont"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is preview of font-size"
        android:layout_marginTop="75dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />
    
    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="254dp"
        android:layout_height="44dp"
        android:layout_marginTop="50dp"
        app:layout_constraintTop_toBottomOf="@+id/changeFont"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.137" />
    
    Run Code Online (Sandbox Code Playgroud)

    1. 转到您的 MainActivity.java。定义您的文本视图和搜索栏。为搜索栏添加更改侦听器。为文本视图动态设置文本大小值。请参阅下面的代码供您参考。

    公共类 MainActivity 扩展 AppCompatActivity {

    TextView view;
    SeekBar bar;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        view = (TextView)findViewById(R.id.changeFont);
        bar = (SeekBar)findViewById(R.id.seekBar);
    
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                view.setTextSize(Float.valueOf(i));
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
        });
    }
    
    Run Code Online (Sandbox Code Playgroud)

    }

查看输出

让我知道,这是你想要的吗?