无法动态设置RelativeLayout的高度
我有问题可以在Android中设置的高度。我想改变的RelativeLayout的高度,以显示一个的LinearLayout它已经定义已经在.XML文件,但将高度设置为并不能看到。下面是详细信息:无法动态设置RelativeLayout的高度
<RelativeLayout android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:focusable="true"
android:focusableInTouchMode="true">
<!--Input Block-->
<RelativeLayout android:id="@+id/input_block"
android:layout_width="fill_parent"
android:layout_height="55dp">
<EditText android:id="@+id/edit_text"
android:layout_marginLeft="10dp"
android:layout_width="220dp"
android:layout_height="55dp"
android:gravity="center"
android:text="Please input here"/>
<Button android:id="@+id/send_btn"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="30dp"
android:text="Send"/>
</RelativeLayout>
<!--File Block-->
<LinearLayout android:id="@+id/file_block"
android:layout_below="@+id/input_block"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:visibility="invisible">
<ImageButton android:id="@+id/emoji_btn"
android:layout_marginLeft="5dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/emoji"
android:background="@drawable/imageBtn_style"/>
<ImageButton android:id="@+id/image_btn"
android:layout_marginLeft="5dp"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="@drawable/imageGallery"
android:background="@drawable/imageBtn_style"/>
</LinearLayout>
</RelativeLayout>
那么,在一个字,该file_block一直隐藏首先,我想edit_ext得到关注后,它会显示。这里是核心代码:
private RelativeLayout inputBlockLayout;
private LinearLayout fileBlockLayout;
private EditText editText;
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
inputBlockLayout = (RelativeLayout)findViewById(R.id.input_block);
fileBlockLayout = (LinearLayout)findViewById(R.id.file_block);
editText = (EditText)findViewById(R.id.edit_text);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//editText get the focus to show the fileBlock
if(hasFocus){
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, inputBlockLayout.getLayoutParams().height + 30);
inptlockLayout.setLayoutParams(relativeLayoutParams);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 30);
fileBlock.setLayoutParams(linearLayoutParams);
fileBlock.setVisibility(View.VISIBLE);
}
else{
//editText lose focus to do sth.
}
}
});
但是我得到的错误:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
我不知道问题出在哪里?或者是否有动态设置布局高度的好方法?
问题的新进展:
首先,感谢@Sagar纳亚克和@Nanoc!我已经申请上解决了原来的错误,并能显示file_block萨加尔纳亚克好方法,但是在那之后,有是有一点点的事情,此前file_block,最高父秀布局RelativeLayout(通过我的邮政编码中的id relative_layout)意外更改。在我真正的项目文件中,relative_layout已应用于layout_alignParentBottom =“真”,并经过file_block显示在顶部显示。
有关详细信息:
XML:
<RelativeLayout android:id="@+id/relative_layout"
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentBottom="true"
android:focusable="true"
android:focusableInTouchMode="true">
<!--File Block-->
<LinearLayout android:id="@+id/file_block"
android:layout_below="@+id/input_block"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
的java:
fileBlockLayout.setVisibility(View.GONE);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
//editText get the focus to show the fileBlock
if(hasFocus){
//inputBlock defined 55dp first, after show the fileBlock it should increase 30dp
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 85);
inputBlockLayout.setLayoutParams(relativeLayoutParams);
fileBlockLayout.setVisibility(View.VISIBLE);
}
}
});
我不知道这个问题是由方法引起setLayoutParamsinputBlock。有没有人有一个好主意?
注:
认为的弹出softKeyBoard可能导致同样的问题(应用性能机器人:windowSoftInputMode = “adjustPan | stateHidden”到活动AndroidManifest.xml没有工作),所以我添加了一个按钮来显示fileBlock,放弃原来的方法。
<!--Input Block-->
<RelativeLayout android:id="@+id/input_block"
android:layout_width="fill_parent"
android:layout_height="55dp">
<ImageButton android:id="@+id/add_file_btn"
android:layout_marginLeft="10dp"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/add_file_btn"
android:layout_centerVertical="true">
<EditText android:id="@+id/edit_text"
android:layout_marginLeft="65dp"
android:layout_width="165dp"
android:layout_height="55dp"
android:gravity="center"
android:text="Please input here"/>
<Button android:id="@+id/send_btn"
android:layout_marginRight="10dp"
android:layout_width="50dp"
android:layout_height="30dp"
android:text="Send"/>
</RelativeLayout>
这里的的.java:
fileBlockLayout.setVisibility(View.GONE);
addFileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 85);
inputBlockLayout.setLayoutParams(relativeLayoutParams);
fileBlockLayout.setVisibility(View.VISIBLE);
}
});
然而,有仍然有问题。 (有没有人有一个很好的方法来解决softKeyBoard弹出的问题,这件事?这两个问题真的让我很烦恼。)
由于logcat错误说,file_block它在一个RelativeLayout里面,所以你必须使用RelativeLayout.LayoutParams。
只要改变你的
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 30);
到
RelativeLayout.LayoutParams linearLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 30);
在接下来的时间,的LayoutParams适用于父视图,所以如果你把一个视图中的LinearLayout内其PARAMS必须是类型的LinearLayout的.LayoutParams。
也许你需要调用requestLayout()来使更改生效。
希望这会有所帮助。
非常感谢@Nanoc!问题真的在这里,LayoutParams应该适用于它的父视图。然而,我想知道是否有其他方法来获得我想要的效果?你有什么好主意吗? –
您正在使用正确的方法,例如,如果需要为更改设置动画效果,则会有所不同。 – Nanoc
首先,检查你的代码在这行 -
inputBlockLayout = (RelativeLayout)findViewById(R.id.input_block);
inputBlockLayout是一个相对的布局。 和input_block是一个线性布局。
更正此错误,并让我知道您面临的下一个问题。
主要问题
你所面临的问题的布局和您所申请的解决方案是非常适用的,但更好的办法来做到这一点会是─
-
做出的LinearLayout的高度file_block正常(你想要的)。
<!--File Block--> <LinearLayout android:id="@+id/file_block" android:layout_below="@+id/input_block" android:layout_width="fill_parent" android:layout_height="30dp" android:orientation="horizontal" android:visibility="invisible"> ... (this is your code as you posted)
-
,那么你在活动改变这一点 -
fileBlockLayout.setVisibility(View.GONE); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { //editText get the focus to show the fileBlock if(hasFocus){ fileBlockLayout.setVisibility(View.VISIBLE); } } });
说明
这种方式较好,那么你申请,因为你是在给空间元素的方法布局,但只是使其高度0dp使其不可见。并且使可见性不可见也不会从布局中消除元素,只是使其不可见,但空间被元素占据。
当您将可见性变为GONE时,该元素将完全从布局中移除,并且在取得可见之前它不会占用任何空间。
让我知道这是否适合你。
我很抱歉,这是我拼错的错误。我放在这里的代码是一个与我的真实项目文件具有相同结构的例子。再次抱歉,我重新编辑了内容。不管怎么说,还是要谢谢你。 –
那么,@Nanoc说的是对的。 LayoutParams应该适用于其父视图。 –
它解决了你的问题吗? –
进行更改后,在此处发布您的整个xml文件。在xml中可能会有一些问题。 –
嗨,@Sagar Nayak!我已经检查过这个问题,它是*** editText ***得到集中引起的*** softKeyBoard ***弹出。但是,在我应用属性*** android:windowSoftInputMode =“adjustPan | stateHidden”***之后,它仍然更改了父布局。所以,我改变了我的方式,通过点击一个按钮来显示*** fileBlock ***,那么问题永远不会得到解决。 –
所以你不想软键盘弹出?那么你什么时候想让它弹出? –