以编程方式更改Textview的高度和宽度 [英] Programatically changing Textview height and width

查看:115
本文介绍了以编程方式更改Textview的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textview,我想将宽度更改为match_parent,将高度更改为wrap_content.它嵌套在水平线性布局中.它是3个文本视图中的第2个,每个文本视图的权重均为1.运行此特定片段时,它将其他两个按钮设置为

I have a textview that I want to change the width to match_parent and height to remain at wrap_content. It is nested within a horizontal linearlayout. It is the 2nd in 3 textviews each of which has a weight of 1. When this particular fragment is run it sets the other two buttons to

previousButton.setVisibility(View.GONE);
nextButton.setVisibility(View.GONE);

TextView

           <TextView
                android:id="@+id/home"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="HOME"
                android:layout_weight="1"
                android:background="@drawable/button_selector"
                android:layout_marginLeft="10dp"
                android:layout_marginBottom="10dp"
                android:padding="10dp"
                android:gravity="center"
                android:textColor="#000000"
                android:textStyle="bold"
                android:onClick="home"
                />

我正在使用以下方法尝试更改片段中的布局:

I am using the following to try to change the layout in a fragment:

homeButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

我运行它时收到错误消息:

When I run it I get the error:

java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

推荐答案

您的TextView父级布局是什么?线性,相对还是什么?样本是否为LinearLayout:

What is your TextView parent layout? Linear, Relative or what? Sample if LinearLayout:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
homeButton.setLayoutParams(params);

您必须基于其父级布局创建参数.

You must create param base on its parent layout.

这篇关于以编程方式更改Textview的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆