无法更改TextView的大小

时间:2016-04-20 07:10:17

标签: android view textview

字体大小已更改,但另一个视图未移动。我不能在这个布局中添加任何元素。我只能修改现有视图,例如我可以更改颜色或文本,但viewHolder.layout.addView(textView)不做任何事情。为什么view大小不会改变?

enter image description here enter image description here

public class StackAdapter extends BaseAdapter...
...
public View getView...
final ViewHolder viewHolder;
...
viewHolder.layout = (LinearLayout) convertView.findViewById(R.id.telephone_1);
...
    viewHolder.layout.post(new Runnable() {
                    @Override
                    public void run() {
                        TextView textView = (TextView)viewHolder.layout.findViewById(R.id.textView11);
                        textView.setText("din1");
                        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);

    //or
                        TextView textView2 = (TextView)viewHolder.layout.findViewById(R.id.textView22);
                        textView2.setText("din2");
                        textView2.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
                        int height_in_pixels2 = textView2.getLineCount() * textView2.getLineHeight(); //approx height text
                        textView2.setHeight(height_in_pixels2);

                    }
                });

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:autofit="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".ExampleActivity">




    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:background="@android:drawable/dialog_holo_light_frame"
        android:id="@+id/card_1"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="60dp"
        android:layout_marginLeft="6dp"
        android:layout_marginRight="6dp"
        android:padding="20dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:weightSum="1"
            android:gravity="top">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:foregroundGravity="left"
                android:gravity="left"
                android:layout_gravity="left">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:id="@+id/imageViewLogoUrl_1"
                    android:foregroundGravity="left"
                    android:scaleType="fitStart"
                    />
            </LinearLayout>

            <android.support.percent.PercentFrameLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="vertical"
                >
            <LinearLayout
                app:layout_heightPercent="65%"
                android:id="@+id/main_telephone_1"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#5191bb">

                <LinearLayout
                    android:id="@+id/telephone_1"
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.8"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="5dp">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Hello"
                        android:id="@+id/textView11"/>
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Hello"
                        android:id="@+id/textView22"/>

                </LinearLayout>

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.1">

                    <TextView
                        android:id="@+id/textViewCompanyName_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:textSize="24sp"
                        android:text="Organization"
                        android:singleLine="true"
                        android:layout_marginBottom="-4dp"/>

                    <TextView
                        android:id="@+id/textViewFullName_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="right"
                        android:textSize="20sp"
                        android:text="Full Name User"
                        android:singleLine="true"
                        android:layout_marginBottom="-4dp"/>

                    <TextView
                        android:id="@+id/textViewJobTitle_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="Job Title"
                        android:gravity="right"
                        android:singleLine="true"
                        android:layout_marginBottom="-4dp"/>

                    <TextView
                        android:id="@+id/textViewDepartment_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textSize="20sp"
                        android:text="Department"
                        android:gravity="right"
                        android:singleLine="true"
                        android:layout_marginBottom="-4dp"/>
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_alignParentBottom="true"
                app:layout_marginTopPercent="65%"
                android:id="@+id/information_1"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#cc6b6b">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="Small Text"
                    android:id="@+id/textView2"/>
            </LinearLayout>
        </android.support.percent.PercentFrameLayout>

        </LinearLayout>

    </LinearLayout>



</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

这适用于我的项目

 textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textView.getTextSize() + 3.0f);

并享受编码......

更改高度或宽度使用此....

  LinearLayout.LayoutParams linearPrams = new   LinearLayout.LayoutParams(LayoutParam.WRAP_CONTENT,LayoutParam.WRAP_CONTENT);

    textView.setLayoutParams(linearPrams);

PercentFrameLayout所有线性布局应以百分比方式......

<android.support.percent.PercentFrameLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <LinearLayout
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout/>
相关问题