当前位置: 移动技术网 > 移动技术>移动开发>Android > 动态计算文字高度,加载不同的布局

动态计算文字高度,加载不同的布局

2020年09月20日  | 移动技术网移动技术  | 我要评论
package com.example.testdemo;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.text.method.ScrollingMovementMethod;import android.view.Display;import android.view.View;import android.view.ViewGroup;import andr
public class CenterActivity extends AppCompatActivity {

    private TextView tvText;
        private String str = "你好";
//    private String str = "scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int i = selectLayout();
//        if (1 == i) {
//            setContentView(R.layout.activity_center);
//        } else if (2 == i) {
//
//        }
        setContentView(R.layout.activity_center2);
        tvText = findViewById(R.id.tvText);
        tvText.setText(str);
        if (i == 1) {
            ViewGroup.LayoutParams layoutParams = tvText.getLayoutParams();
            if (layoutParams != null) {
                layoutParams.height = 1550;
                tvText.setLayoutParams(layoutParams);
            }
        }

//        tvText.setMovementMethod(ScrollingMovementMethod.getInstance());
//        System.out.println("lgj measureHeight : " + measuredHeight);
//        ViewGroup.LayoutParams layoutParams = tvText.getLayoutParams();
//        System.out.println("lgj layoutParams :" + layoutParams);
//        if (layoutParams != null){
//            layoutParams.height = measuredHeight;
//            tvText.setLayoutParams(layoutParams);
//            tvText.requestLayout();
//        }

        }

        private int selectLayout () {
            TextView textView = new TextView(this);
            textView.setText(str);
            Display defaultDisplay = getWindowManager().getDefaultDisplay();
            int width = defaultDisplay.getWidth();
            int height1 = defaultDisplay.getHeight();
            //给宽度加上设置条件,最多只有屏幕宽
            int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);
            textView.measure(widthMeasureSpec, 0);
            int measuredHeight = textView.getMeasuredHeight();
            if (measuredHeight > height1) {
                return 2;
            }
            return 1;
        }
    }
package com.example.testdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class CenterActivity extends AppCompatActivity {

    private TextView tvText;
//    private String str = "你好";
    private String str = "scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)scrollview 在使用过程中的问题(scrollview 作为父控件时子控件无法展示的问题)";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        int i = selectLayout();
        if (1 == i){
            setContentView(R.layout.activity_center);
        }else {
            setContentView(R.layout.activity_center2);
        }
        tvText = findViewById(R.id.tvText);
        tvText.setText(str);
//        tvText.setMovementMethod(ScrollingMovementMethod.getInstance());
//        System.out.println("lgj measureHeight : " + measuredHeight);
//        ViewGroup.LayoutParams layoutParams = tvText.getLayoutParams();
//        System.out.println("lgj layoutParams :" + layoutParams);
//        if (layoutParams != null){
//            layoutParams.height = measuredHeight;
//            tvText.setLayoutParams(layoutParams);
//            tvText.requestLayout();
//        }

    }

    private int selectLayout() {
        TextView textView = new TextView(this);
        textView.setText(str);
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        int width = defaultDisplay.getWidth();
        int height1 = defaultDisplay.getHeight();
        //给宽度加上设置条件,最多只有屏幕宽
        int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.AT_MOST);
        textView.measure(widthMeasureSpec,0);
        int measuredHeight = textView.getMeasuredHeight();
        if (measuredHeight > height1){
            return 2;
        }
        return 1;
    }
}

布局代码activity_center

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="20dp"
            android:layout_marginBottom="20dp"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_marginRight="20dp"
            android:layout_marginBottom="20dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/tvText"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/iv"
            android:gravity="center"
            android:scrollbars="vertical"
            android:text="你好"
            android:textSize="20dp" />
    </RelativeLayout>
</ScrollView>

布局代码activity_center2

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:fillViewport="true"
    android:layout_height="match_parent">

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

        <TextView
            android:id="@+id/tvText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/iv"
            android:gravity="center"
            android:text="你好"
            android:textSize="20dp" />
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_marginBottom="20dp"
            android:layout_height="72dp">
            <ImageView
                android:id="@+id/iv"
                android:layout_width="52dp"
                android:layout_height="52dp"
                android:layout_alignParentBottom="true"
                android:layout_marginLeft="20dp"
                android:src="@mipmap/ic_launcher" />
            <ImageView
                android:layout_width="52dp"
                android:layout_height="52dp"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="20dp"
                android:src="@mipmap/ic_launcher" />

        </RelativeLayout>
    </LinearLayout>
</ScrollView>

本文地址:https://blog.csdn.net/weixin_39413778/article/details/108695132

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网