我有问题可以在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弹出的问题,这件事?这两个问题真的让我很烦恼。)
进行更改后,在此处发布您的整个xml文件。在xml中可能会有一些问题。 –
嗨,@Sagar Nayak!我已经检查过这个问题,它是*** editText ***得到集中引起的*** softKeyBoard ***弹出。但是,在我应用属性*** android:windowSoftInputMode =“adjustPan | stateHidden”***之后,它仍然更改了父布局。所以,我改变了我的方式,通过点击一个按钮来显示*** fileBlock ***,那么问题永远不会得到解决。 –
所以你不想软键盘弹出?那么你什么时候想让它弹出? –