2016-03-28 275 views
0

我有問題可以在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彈出的問題,這件事?這兩個問題真的讓我很煩惱。)

+0

進行更改後,在此處發佈您的整個xml文件。在xml中可能會有一些問題。 –

+0

嗨,@Sagar Nayak!我已經檢查過這個問題,它是*** editText ***得到集中引起的*** softKeyBoard ***彈出。但是,在我應用屬性*** android:windowSoftInputMode =「adjustPan | stateHidden」***之後,它仍然更改了父佈局。所以,我改變了我的方式,通過點擊一個按鈕來顯示*** fileBlock ***,那麼問題永遠不會得到解決。 –

+0

所以你不想軟鍵盤彈出?那麼你什麼時候想讓它彈出? –

回答

1

由於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()來使更改生效。

希望這會有所幫助。

+0

非常感謝@Nanoc!問題真的在這裏,LayoutParams應該適用於它的父視圖。然而,我想知道是否有其他方法來獲得我想要的效果?你有什麼好主意嗎? –

+0

您正在使用正確的方法,例如,如果需要爲更改設置動畫效果,則會有所不同。 – Nanoc

1

首先,檢查你的代碼在這行 -

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時,該元素將完全從佈局中移除,並且在取得可見之前它不會佔用任何空間。

讓我知道這是否適合你。

+0

我很抱歉,這是我拼錯的錯誤。我放在這裏的代碼是一個與我的真實項目文件具有相同結構的例子。再次抱歉,我重新編輯了內容。不管怎麼說,還是要謝謝你。 –

+0

那麼,@Nanoc說的是對的。 LayoutParams應該適用於其父視圖。 –

+0

它解決了你的問題嗎? –