对于我的GridView使用BaseAdapter,字体大小有时很大,有时很小

时间:2016-08-03 09:52:03

标签: android android-gridview baseadapter

首先,我使用setTextSize(),然后使用setText()作为我的TextViewsetText()的默认字体大小必须包含我在setTextSize()中调用的字体大小。但是getView()的方法返回一个视图来显示我的GridView项目,所以我认为字体大小有时不大,有时很小。造成这种情况的原因是什么?

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder viewHolder = null;
    if (convertView == null) {
        viewHolder = new ViewHolder();
        convertView = LayoutInflater.from(mContext)
                .inflate(R.layout.gridview_monitoring_item,null);
        viewHolder.mImageMark = (ImageView) convertView
                .findViewById(R.id.gridview_site_mark);
        viewHolder.mSiteIdText = (TextView) convertView
                .findViewById(R.id.gridview_site_id);
        viewHolder.mStatusText = (TextView) convertView
                .findViewById(R.id.gridview_status);
        viewHolder.siteNameTextView = (TextView) convertView
                .findViewById(R.id.gridview_site_name);
        convertView.setTag(viewHolder);
    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    Map<Integer,String> map = mMonitoringData.get(position);
    if (map.get(R.id.gridview_site_mark).equals("server")) {
        viewHolder.mImageMark.setImageDrawable(mContext.getResources().getDrawable(
                R.drawable.ic_server));
    } else if (map.get(R.id.gridview_site_mark).equals("website")) {
        viewHolder.mImageMark.setImageDrawable(mContext.getResources().getDrawable(
                R.drawable.ic_website));
    }

    viewHolder.mStatusText.setText(map.get(R.id.gridview_status));
    viewHolder.mSiteIdText.setText(map.get(R.id.gridview_site_id));
    viewHolder.siteNameTextView.setText(map.get(R.id.gridview_site_name));

    if (mContext.getString(R.string.unknown).equals(map.get(R.id.gridview_status))) {   //如果是unknown
        convertView.setBackgroundResource(R.color.siteUnknowBackground);
    } else if (map.get(R.id.gridview_status).contains("OK") || map.get(R.id.gridview_status).contains("open")) {
        convertView.setBackgroundResource(R.color.siteRunningBackground);
        viewHolder.siteNameTextView.setTextColor(mContext.getResources().getColor(
                R.color.siteRunningNameTextColor));
        viewHolder.mStatusText.setTextColor(mContext.getResources().getColor(
                R.color.siteRunningStatusTextColor));
    }

    if (map.get(IS_NOMAL).equals("false") =) {  //有log且站点不正常时,显示出红色
        convertView.setBackgroundResource(R.color.siteDownBackground);
        viewHolder.siteNameTextView.setTextColor(mContext.getResources()
                .getColor(R.color.siteDownNameTextColor));
        viewHolder.mStatusText.setTextColor(mContext.getResources().getColor(
                R.color.siteDownStatusTextColor));
    }

    /**
     * 设置字体大小
     */
    viewHolder.mStatusText.setTextSize(mContext.getResources().getDimension(
            R.dimen.status_text_small));
    viewHolder.mSiteIdText.setTextSize(mContext.getResources().getDimension(
            R.dimen.status_text_small));

    if (PublicFunction.isTablet(mContext)) {
        int x = PublicFunction.getXdpi(mContext);
        viewHolder.mStatusText.setPadding(0, (x / 2), 0, 0);
        viewHolder.mStatusText.setTextSize(14);
        viewHolder.mSiteIdText.setTextSize(14);

        if (viewHolder.siteNameTextView.length() > 10
                && viewHolder.siteNameTextView.length() < 14) {
            viewHolder.siteNameTextView.setTextSize(26);
        } else if (viewHolder.siteNameTextView.length() > 13) {
            viewHolder.siteNameTextView.setTextSize(22);
        } else {
            viewHolder.siteNameTextView.setTextSize(32);
        }
    } else {
        if (viewHolder.siteNameTextView.length() > 10
                && viewHolder.siteNameTextView.length() < 14) {
            viewHolder.siteNameTextView.setTextSize(20);
        } else if (viewHolder.siteNameTextView.length() > 13) {
            viewHolder.siteNameTextView.setTextSize(16);
        } else {
            viewHolder.siteNameTextView.setTextSize(26);
        }
    }

    /**
     * 设置字体颜色
     */
    viewHolder.siteNameTextView.setTextColor(mContext.getResources().getColor(
            R.color.siteRunningNameTextColor));
    viewHolder.mStatusText.setTextColor(mContext.getResources().getColor(
            R.color.siteRunningStatusTextColor));

    return convertView;

}`

1 个答案:

答案 0 :(得分:0)

假设您要将字体大小设置为 <!-- edittext line color --> <item name="colorControlNormal">@color/white_four</item> <item name="colorControlActivated">@color/greyish_brown</item> <item name="editTextStyle">@style/App_EditTextStyle</item> <!-- For compatibility with the support library --> </style> 单位,必须在dp方法

中传递单位

如果您想使用setTextSize

设置文字大小
20dp

或20px

textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);