Java 单击即可更改视图高度

Java 单击即可更改视图高度,java,android,xml,onclick,height,Java,Android,Xml,Onclick,Height,我有一个文本视图,目前是0dp高,我希望它得到20dp高每点击一个按钮 <TextView android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/colorPrimary"/> <Button an

我有一个文本视图,目前是0dp高,我希望它得到20dp高每点击一个按钮

 <TextView
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorPrimary"/>

<Button
    android:layout_centerHorizontal="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

您可以通过编程方式更改textview的高度,如下所示-

LayoutParams params = (LayoutParams) textView.getLayoutParams();
params.height = 20;
textView.setLayoutParams(params);

现在,您可以获得以前的高度,并在每次单击按钮时向其添加20。

您可以在java中通过SharedReference更改您的高度或大小,只需在android中的按钮的onClick方法中声明SharedReference,如下所述,您的textView大小将在单击按钮时更改:

主活动
Button small=(Button)findViewById(R.id.small);
small.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
TextView tv=(TextView)findViewById(R.id.teext);
SharedReferences SharedReferences=PreferenceManager.GetDefaultSharedReferences(MainActivity.this.getBaseContext());
tv.setTextSize(sharedPreferences.getFloat(“FONT_SIZE”,30/*默认值*/);
}

});如果是0dp高,应该如何单击?按钮(不是0dp高)应该被单击,文本视图(是0dp高)不应该被点击。您应该修改您的问题。它说视图是0dp高,并且每点击一个按钮应该高出20dp。我不知道为什么要用LayoutParams来更新UI。另一方面是用来设置直接布局高度的。我的代码可以处理静态数据,但当我使用room db从db获取数据时,更新UI就不起作用了。这里是kotlin V11.layoutParams.height=dpToPx(pointsValue[0])中的代码,但只要我在一个视图上使用了LayoutParam,所有这些都可以工作。我不知道原因