使用Android Studio中的按钮更改字体大小

时间:2018-07-09 21:03:25

标签: android

我的布局中包含波纹管[带有共享按钮,并在上方具有增减按钮]

preview image

现在,我想编写一个使增加和减少按钮使文本的字体大小更改的代码

请问怎么走

xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app ="http://schemas.android.com/apk/res-auto">

<item android:id="@+id/share"
    android:title="مشاركة الذكر"
    app:showAsAction="always"
    android:icon="@drawable/share_icon">
</item>
<item android:id="@+id/increase_text"
    android:title="تكبير الخط"
    app:showAsAction="always"
    android:icon="@drawable/increas_icon">
</item>
<item  android:id="@+id/decrease_text"
    android:title="تصغير الخط"
    app:showAsAction="always"
    android:icon="@drawable/decrease_icon">
</item>
<item  android:id="@+id/coppy_text"
    android:title="نسخ النص"
    app:showAsAction="always"
    android:icon="@drawable/coppy_icon">
</item>   

这是主要代码

@Override
public boolean onCreateOptionsMenu (Menu menu){
    getMenuInflater().inflate(R.menu.menu1,menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();

    if (id==R.id.share)
        Toast.makeText(this,"share",Toast.LENGTH_SHORT).show();
    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:0)

使用以下代码

//init your textview and an int to record the textsize and change in textsize int
private TextView myTextView;
private int textSize = 20; //lets say default font size is 20sp
private int diff = 5;

//define your textview

//now the main part

@Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if(id == R.id.increase_text){
        textSize = textSize+diff;
        myTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    }else if(id == R.id.decrease_text){
        textSize = textSize-diff;
        myTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    }
}

希望有帮助