2016-10-19 97 views
0

我有一个textview,我想将宽度更改为match_parent和高度以保留在wrap_content中。它嵌套在水平线性布局中。它是在3个textviews其中的每一个具有1的权重的第二当该特定片段运行它设置其他两个按钮以编程方式更改Textview的高度和宽度

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" 
       /> 

我使用下面给尝试在一个片段改变布局:

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

当我运行它,我得到的错误:

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

错误相当明显。你不能将'ViewGroup'强制转换为'LinearLayout'。所以我认为你的'TextView'的父级布局是'Linear'? –

回答

2

什么是您的TextView父级布局?线性,相对还是什么? 样品如果LinearLayout中:

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

您必须创建其父布局PARAM基地。

0

假设你的父母是LinearLayout

LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
homeButton.setLayoutParams(layoutParam); 
0

你已经给了layout_width = “0dp” 和layout_weight = “1”。所以,当其他两个按钮将隐藏。此主页按钮将占满全部宽度。但View.INVISIBLE不会将它们从宽度上移除。你应该使用View.GONE,这样即使它们不可见,它们也不应该占用宽度。

无形:

This view is invisible, but it still takes up space for layout purposes. 

GONE:

This view is invisible, and it doesn't take any space for layout purposes. 

你并不需要在首页TextView的重新设置布局PARAMS。

+0

开发人员已经以编程方式使用View.GONE属性 – Madhan

相关问题