简体   繁体   English

无法更改TextView的大小

[英]Cant change size of TextView

Font size is changed but the other view doesn't move. 字体大小已更改,但其他视图未移动。 I cant add In this layout any elements. 我无法在此布局中添加任何元素。 I can only modify existing view, for example i can change the color or text but viewHolder.layout.addView(textView) doesn't do anything . 我只能修改现有的视图,例如,我可以更改颜色或文本,但是viewHolder.layout.addView(textView)不执行任何操作。 And why view size doesn't change? 为什么view大小不变?

在此处输入图片说明 在此处输入图片说明

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文件

<?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>

this is work for my project 这是我的项目的工作

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

and enjoy coding ...... 并享受编码......

for changing the height or width use this .... 要更改高度或宽度,请使用此...。

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

    textView.setLayoutParams(linearPrams);

PercentFrameLayout all linear layout should be in percentage manner ...... 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/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM