qq634416025的专栏

我在网上搜了个sidebar的源码,, 但是在xml里面开启大屏模式后,导航栏字体变得很小

<supports-screensandroid:anyDensity="true"android:largeScreens="true"android:normalScreens="true"android:smallScreens="true"android:xlargeScreens="true" />本来正常的应该是:

但是在我的note4上面却显示这样

很小的字体,看了下源码 , 原来是原作者把字体写死了。

// 获取焦点改变背景颜色.int height = getHeight();// 获取对应高度int width = getWidth(); // 获取对应宽度int singleHeight = height / b.length;// 获取每一个字母的高度for (int i = 0; i < b.length; i++) {paint.setColor(Color.rgb(33, 65, 98));// paint.setColor(Color.WHITE);paint.setTypeface(Typeface.DEFAULT_BOLD);paint.setAntiAlias(true);paint.setTextSize(20);// 选中的状态if (i == choose) {paint.setColor(Color.parseColor("#3399ff"));paint.setFakeBoldText(true);}// x坐标等于中间-字符串宽度的一半.float xPos = width / 2 – paint.measureText(b[i]) / 2;float yPos = singleHeight * i + singleHeight;canvas.drawText(b[i], xPos, yPos, paint);paint.reset();// 重置画笔

然后我改了下,把字体设置成相对大小的。

/** * 重写这个方法 */protected void onDraw(Canvas canvas) {super.onDraw(canvas);if (itemHeight == -1) {itemHeight = getHeight() / b.length;}// 获取焦点改变背景颜色.int height = getHeight();// 获取对应高度int width = getWidth(); // 获取对应宽度int singleHeight = height / b.length;// 获取每一个字母的高度for (int i = 0; i < b.length; i++) {paint.setColor(Color.rgb(33, 65, 98));// paint.setColor(Color.WHITE);paint.setTypeface(Typeface.DEFAULT_BOLD);paint.setAntiAlias(true);//paint.setTextSize(20);paint.setTextSize(itemHeight – 10);// 选中的状态if (i == choose) {paint.setColor(Color.parseColor("#3399ff"));paint.setFakeBoldText(true);}// x坐标等于中间-字符串宽度的一半.float xPos = width / 2 – paint.measureText(b[i]) / 2;float yPos = singleHeight * i + singleHeight;canvas.drawText(b[i], xPos, yPos, paint);paint.reset();// 重置画笔}}完整的源码是:

package net.mwplay.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Typeface;import android.graphics.drawable.ColorDrawable;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.TextView;public class SideBar extends View {// 触摸事件private OnTouchingLetterChangedListener onTouchingLetterChangedListener;// 26个字母public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z", "#" };private int choose = -1;// 选中private Paint paint = new Paint();private TextView mTextDialog;private float itemHeight = -1;/** * 为SideBar设置显示字母的TextView * * @param mTextDialog */public void setTextView(TextView mTextDialog) {this.mTextDialog = mTextDialog;}public SideBar(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public SideBar(Context context, AttributeSet attrs) {super(context, attrs);}public SideBar(Context context) {super(context);}/** * 重写这个方法 */protected void onDraw(Canvas canvas) {super.onDraw(canvas);if (itemHeight == -1) {itemHeight = getHeight() / b.length;}// 获取焦点改变背景颜色.int height = getHeight();// 获取对应高度int width = getWidth(); // 获取对应宽度int singleHeight = height / b.length;// 获取每一个字母的高度for (int i = 0; i < b.length; i++) {paint.setColor(Color.rgb(33, 65, 98));// paint.setColor(Color.WHITE);paint.setTypeface(Typeface.DEFAULT_BOLD);paint.setAntiAlias(true);//paint.setTextSize(20);paint.setTextSize(itemHeight – 10);// 选中的状态if (i == choose) {paint.setColor(Color.parseColor("#3399ff"));paint.setFakeBoldText(true);}// x坐标等于中间-字符串宽度的一半.float xPos = width / 2 – paint.measureText(b[i]) / 2;float yPos = singleHeight * i + singleHeight;canvas.drawText(b[i], xPos, yPos, paint);paint.reset();// 重置画笔}}@Overridepublic boolean dispatchTouchEvent(MotionEvent event) {final int action = event.getAction();final float y = event.getY();// 点击y坐标final int oldChoose = choose;final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.switch (action) {case MotionEvent.ACTION_UP:setBackgroundDrawable(new ColorDrawable(0x00000000));choose = -1;//invalidate();if (mTextDialog != null) {mTextDialog.setVisibility(View.INVISIBLE);}break;default:// setBackgroundResource(R.drawable.sidebar_background);if (oldChoose != c) {if (c >= 0 && c < b.length) {if (listener != null) {listener.onTouchingLetterChanged(b[c]);}if (mTextDialog != null) {mTextDialog.setText(b[c]);mTextDialog.setVisibility(View.VISIBLE);}choose = c;invalidate();}}break;}return true;}/** * 向外公开的方法 * * @param onTouchingLetterChangedListener */public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) {this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;}/** * 接口 * * @author coder * */public interface OnTouchingLetterChangedListener {public void onTouchingLetterChanged(String s);}}没有其他意思,只是想记录下自己犯过的错。希望对大家有帮助。

为我祈祷平安就好。我的旅行,会有你们的故事陪伴,所以我不会孤单。放心吧。

qq634416025的专栏

相关文章:

你感兴趣的文章:

标签云: