如何动态更改视图的尺寸

问题描述 投票:0回答:1

我有一个名为 TextCell 的视图,它宽 100sp,高 35sp,还有一个宽 50sp 的垂直线性布局,当我按下按钮时,它会向布局添加 x 数量的 TextCell。

text_cell.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:foregroundGravity="center"
        tools:ignore="MissingConstraints">

        <View
            android:id="@+id/black_frame"
            android:layout_width="100sp"
            android:layout_height="35sp"
            android:background="@color/black"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/color_box"
            app:layout_constraintBottom_toBottomOf="@+id/black_frame"
            app:layout_constraintEnd_toEndOf="@+id/black_frame"
            app:layout_constraintStart_toStartOf="@+id/black_frame"
            app:layout_constraintTop_toTopOf="@+id/black_frame"
            android:layout_width="98sp"
            android:layout_height="33sp"
            android:background="#ffffff"
            android:gravity="center"
            android:textColor="@color/black"
            android:text=""
            android:autoSizeTextType="uniform"
            android:lines="1"
            android:padding="5sp"
            android:textStyle="bold"
            android:textAlignment="center"/>

    </androidx.constraintlayout.widget.ConstraintLayout>



</androidx.constraintlayout.widget.ConstraintLayout>

我的 TextCell.java 类:

package com.example.teacherplanner;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TextCell extends RelativeLayout {

    public TextCell(Context context,String text,String color) {
        super(context);
        init(context,text,color);
    }
    public TextCell(Context context,String text,String color, AttributeSet attrs) {
        super(context, attrs);
        init(context,text,color);
    }
    private void init(Context context, String text, String color) {
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.text_cell,this,true);

        TextView TextInside = findViewById(R.id.color_box);

        TextInside.setText(text);
        TextInside.setBackgroundColor(Color.parseColor(color));
    }
}

当它添加到布局中时,如何将其设置为 50sp 宽,而不在 xml 文件中将其设置为 50sp bc 我在其他地方使用它,并且我需要它原样

我尝试使用 LayoutParams:

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
timestamp.setLayoutParams(new ViewGroup.LayoutParams(
                          ViewGroup.LayoutParams.MATCH_PARENT,
                          ViewGroup.LayoutParams.WRAP_CONTENT
));
TimeStamps.addView(timestamp);

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
int widthInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50,  getResources().getDisplayMetrics());

int heightInPixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics());

ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(widthInPixels, heightInPixels);
timestamp.setLayoutParams(layoutParams);
TimeStamps.addView(timestamp);

但它保持 100sp 宽度

java android android-custom-view layoutparams
1个回答
0
投票

我看到您使用

LayoutParams
当您尝试动态修改视图的宽度和高度时它应该可以工作,但我也注意到您已经在
text_cell.xml
中设置了宽度和高度,因此您的尺寸被覆盖当你的布局膨胀时你的 XML。

您有 2 个选择:

第一个是在膨胀布局后以编程方式调整尺寸。

private void init(Context context, String text, String color) {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.text_cell, this, true);

TextView TextInside = findViewById(R.id.color_box);
View blackFrame = findViewById(R.id.black_frame);

// SP to px
int newWidthInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50, getResources().getDisplayMetrics());
int newHeightInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 35, getResources().getDisplayMetrics());

// new dimensions
blackFrame.getLayoutParams().width = newWidthInPx;
blackFrame.getLayoutParams().height = newHeightInPx;
blackFrame.requestLayout(); 

TextInside.setText(text);
TextInside.setBackgroundColor(Color.parseColor(color)); }

第二步是在将尺寸添加到父级之前修改尺寸。

TextCell timestamp = new TextCell(WeekEditing.this,"test","#FFD54F");
int newWidthInPx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 50, getResources().getDisplayMetrics());
timestamp.getLayoutParams().width = newWidthInPx;
TimeStamps.addView(timestamp);

如果有效的话,现在就尝试一下。

© www.soinside.com 2019 - 2024. All rights reserved.