【问题标题】:How to set different width in textView如何在textView中设置不同的宽度
【发布时间】:2012-04-24 07:19:25
【问题描述】:

我创建了一个自定义ListView,其中添加了两个TextView,其中一个TextView 显示日期,另一个TextView 显示在该特定日期输入的重量。现在我想要的是在第二个TextView 显示权重,文本视图的宽度设置为用户输入的权重。我写的代码显示了一些问题。这意味着当用户输入重量 70 和 75 时,TextView 的宽度显示相同的大小。为什么会这样。请任何人帮我解决这个问题。

列表活动的 XML 代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/img_allnotes_bg" >
       
    <TextView
        android:id="@+id/txt_weight_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_divder1"
        android:layout_marginTop="15dp"
        android:gravity="center_horizontal|center_vertical"
        android:text="@string/txt_weight_title"
        android:textColor="#5f5f5f"
        android:textSize="@dimen/font_size_page_text" >
    </TextView>


    <ListView
        android:id="@+id/weight_list_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/bottom_control_bar"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/image_divider2"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="2dp"
        android:cacheColorHint="#00000000"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:listSelector="#00000000" >
    </ListView>  
</RelativeLayout>

ListView 项的 XML 代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="2dip" >

    <TextView
        android:id="@+id/txt_weightdate_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:focusable="false"
        android:textColor="#5f5f5f"
        android:textSize="@dimen/font_size_page_text" >
    </TextView>
     <TextView
        android:id="@+id/txt_weight_display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_weightdate_display"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="60dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:focusable="false"
        android:textColor="#5f5f5f"
        android:background="#00FFFF"
        android:textSize="@dimen/font_size_page_text" >
    </TextView>
</RelativeLayout>

用于生成列表视图的 Java 代码

public void displayWeights()
{
    m_allWeightsList  = (ListView) findViewById(R.id.weight_list_view);
            
    int iWeightCount = CycleManager.getSingletonObject().getWeightCount();
    
    m_weightsdateDisplay  = new String[iWeightCount];
    
    m_weightDisplay = new String[iWeightCount];
    
    m_enStage = new int[iWeightCount];
    
    int i;      
    
    for (i=0; i<iWeightCount; i++)
    {
        m_weightsdateDisplay[i] = "";

        Date dtWeightDate = CycleManager.getSingletonObject().getWeightDate(i);
        
        m_enStage[i] = CycleManager.getSingletonObject().getCycleStage(dtWeightDate);

        m_weightsdateDisplay[i] = formator.format(dtWeightDate.getTime());
        
        m_weightDisplay[i] = "";

        m_weightDisplay[i] = CycleManager.getSingletonObject().getWeight(dtWeightDate); 
    }
    
    m_adapter = new AllWeightsAdapter(this, m_weightsdateDisplay, m_weightDisplay);

    m_allWeightsList.setAdapter(m_adapter);  
    
}

class AllWeightsAdapter extends BaseAdapter
{   
    public String    m_weightsDateDisplay[];
    
    public String    m_weightsDisplay[];

    public Activity  m_context;

    public LayoutInflater  m_inflater;

    public AllWeightsAdapter(Activity m_context, String[] m_weightsDateDisplay, String[] m_weightsDisplay)
    {
        super();

        this.m_context = m_context;

        this.m_weightsDateDisplay = m_weightsDateDisplay;
        
        this.m_weightsDisplay = m_weightsDisplay;

        this.m_inflater = (LayoutInflater)m_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);      
    }
    public AllWeightsAdapter()
    {
        
    }
    public int getCount()
    {
        return m_weightsDateDisplay.length; 
    }

    public Object getItem(int position)
    {   
        return null;
    }

    public long getItemId(int position)
    {
        return 0;
    }

    public class ViewHolder
    {
        TextView txt_weightdt_display;
        
        TextView txt_weight_display;
    }

    @Override 
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
                
        Typeface face = Typeface.createFromAsset(m_context.getAssets(), "fonts/Cicle Semi.ttf");
        
        if(convertView == null)
        {
            holder = new ViewHolder();
            convertView = m_inflater.inflate(R.layout.all_weights_list, null);

            holder.txt_weightdt_display = (TextView) convertView.findViewById(R.id.txt_weightdate_display); 
            holder.txt_weight_display = (TextView) convertView.findViewById(R.id.txt_weight_display);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder)convertView.getTag();
        }
        
        // Added Weight Date in List View Component
        holder.txt_weightdt_display.setText(m_weightsDateDisplay[position]);
        holder.txt_weightdt_display.setTypeface(face);
        
        // Added Weights in List View   Component       
        double factor, width, scale, borderwidth, minScreenWidth = 40, maxScreenWidth = 200;
        double maxWeight = CycleManager.getSingletonObject().getMaxWeight();
        double minWidth  = CycleManager.getSingletonObject().getMinWeight();  
        
        scale = maxWeight - minWidth;
        factor = (maxScreenWidth - minScreenWidth) / scale;

        width = (maxWeight - Double.parseDouble(m_weightsDisplay[position])) * factor;
        borderwidth = maxScreenWidth - width;

        holder.txt_weight_display.setText(m_weightsDisplay[position]);
        holder.txt_weight_display.setMinWidth((int)borderwidth);
        holder.txt_weight_display.setTypeface(face);
                
        return convertView;
    } 
}
}

截图

【问题讨论】:

  • 我猜 70 >= 视图的最大尺寸。尝试一些导致该值的值,例如 60、65。如果是这样,则可以将宽度更改为一半或避免达到最大尺寸
  • 但是视图宽度是换行内容..如何解决我的问题,以便我输入的任何重量都可以调整宽度...
  • 更改缩放比例,使其长度更小。您可能也可以使用视图为该元素提供更多空间
  • 哪个缩放 r 谈论..实际上我没有找到任何线索如何解决它..你能解释我解决这个问题吗

标签: android listview textview


【解决方案1】:

每个 60dps 的左右边距可能是问题所在。您为空白空间花费了 120dps。 http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html 表示手机的屏幕尺寸为 320dp 宽度。

此外,我不确定 CycleManager 是做什么的,但如果你不这样做,可能会定义你应该使用设备屏幕宽度并相应缩放的最大值。

最后,我想这无关紧要,但您正在使用 wrap_content 作为列表视图。你不觉得把它设为fill_parent更合适吗?

【讨论】:

    【解决方案2】:

    好的@Anshuman 这是屏幕现实的问题。 70 和 75 的大小相同,因为您已经用尽了该视图的空间。尝试改变那个蓝色标记的比例。也许这样的事情会给你更多的空间(只需使用 shrinkScaler 变量,直到你获得适合所有你想要的权重所需的大小。

    double shrinkScaler = .75;
    holder.txt_weight_display.setMinWidth((int)(borderwidth * shrinkScaler));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多