我有問題可以在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 ***,那麼問題永遠不會得到解決。 –
所以你不想軟鍵盤彈出?那麼你什麼時候想讓它彈出? –