在listview中更改Textview Size Runtime

时间:2012-12-24 07:30:24

标签: android android-listview textview

我想动态更改textview大小运行时在listview中使用,但是在获取屏幕宽度运行后我获得屏幕宽度为6部分之后,我对文本视图没有任何影响我将从屏幕宽度分配textview大小但是此textview具有不同的布局,因此如何在我的布局中采用此布局并更改textview运行时的大小

这里我把我的布局

Home.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <LinearLayout 
                android:id="@+id/textlayout" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="12"
                android:layout_weight="1"
                android:gravity="center">

                <TextView
                    android:id="@+id/text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:background="@drawable/flightlist"
                    android:text="Destination"
                    android:textSize="17dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/text2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Flight"

                    android:textSize="17dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/text3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:background="@drawable/flightlist"
                    android:text="Airlines"
                    android:textSize="17dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/text4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    android:gravity="center"
                    android:text="Schedule"
                    android:textSize="17dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/text5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Term Gate"
                    android:textSize="17dp"
                    android:textStyle="bold"
                    android:background="@drawable/flightlist" />

                <TextView
                    android:id="@+id/text6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Status"
                    android:textSize="17dp"
                    android:textStyle="bold" />

             </LinearLayout>

              <ListView
                    android:id="@+id/list"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_weight="10"
             >
             </ListView>




</LinearLayout>

list.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="6" 
    android:gravity="center">

    <TextView
        android:id="@+id/textdestination"
        android:layout_width="183dp"
        android:layout_height="fill_parent"
        android:text="Destination"
        android:textSize="15dp"
        android:gravity="center"

        />

    <TextView
        android:id="@+id/textflight"
        android:layout_width="105dp"
        android:layout_height="fill_parent"
        android:text="Flight"
        android:textSize="15dp"
        android:gravity="center"
        android:background="@drawable/flightlist"
         />

    <TextView
        android:id="@+id/textairline"
        android:layout_width="185dp"
        android:layout_height="fill_parent"
        android:text="Airlines"
        android:textSize="15dp" 
        android:gravity="center"/>

            <TextView
                android:id="@+id/textschedule"
                android:layout_width="73dp"
                android:layout_height="fill_parent"
                android:text="Schedule"
                android:textSize="15dp"
                android:gravity="center"
                android:background="@drawable/flightlist"
            />

            <TextView
                android:id="@+id/texttermgate"
                android:layout_width="105dp"
                android:layout_height="fill_parent"
                android:text="Term Gate"
                android:textSize="15dp"
                android:gravity="center"
                />


            <TextView
                android:id="@+id/textstatus"
                android:layout_width="73dp"
                android:layout_height="fill_parent"
                android:text="Status"
                android:textSize="15dp" 
                android:gravity="center"
                android:background="@drawable/flightlist"/>

</LinearLayout>

HomeActivity.java

View inflater=getLayoutInflater().inflate(R.layout.list2, list, false);
    TextView textdestination=(TextView)inflater.findViewById(R.id.textdestination);
    textdestination.setHeight(50);
    textdestination.setWidth((textlayoutwidth*25)/100);
    textdestination.setTextSize(100);

2 个答案:

答案 0 :(得分:1)

您可以使用以下命令更改运行时的textview宽度和高度:

textdestination.setWidth(pixels);
textdestination.setHeight(pixels); 

答案 1 :(得分:1)

您可以在用于ListView的适配器getView()方法中执行此操作。

mInflater = (LayoutInflater) mActivity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;
    if (convertView == null) {

        holder = new ViewHolder();

        convertView = mInflater.inflate(R.layout.list, parent,
                false);
        holder.tv1 = (TextView) convertView
                .findViewById(R.id.textdestination);
        holder.tv2 = (TextView) convertView
                .findViewById(R.id.textflight);
        holder.tv3 = (TextView) convertView
                .findViewById(R.id.textairline);
        .......
        .......
        .......

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if(someCondition){

    holder.tv1.setLayoutParams(new LinearLayout.LayoutParams(50,50));
    holder.tv2setLayoutParams(new LinearLayout.LayoutParams(50,50));
    holder.tv3.setLayoutParams(new LinearLayout.LayoutParams(50,50));
    .....
    .....
    .....
    }

    return convertView;
}