2013-01-31 13 views
3

我想我在這裏缺少一些基本的東西。看看你們有沒有人可以幫忙。重量的變化沒有在佈局中起作用

我有一個活動(activity_story1)這裏我加載一些文本和其他一些控制。我列出所有組件的方式是將它們放入一個LinearLayout中的RelativeLayout組中,然後爲這些RelativeLayouts分配權重。下面的示例xml供參考。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".Story1Activity" 
android:id="@+id/StoryLinearLayout"> 

<TextView 
     android:id="@+id/titleStory" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.05"/> 

<TextView 
     android:id="@+id/textViewStory1" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip" 
     android:layout_weight="0.90"/> 

<LinearLayout 
     android:id="@+id/footerStoryLinearLayout" 
     android:layout_weight="0.05" 
     android:layout_width="fill_parent" 
     android:layout_height="0dip"> 
     <RelativeLayout 
      android:id="@+id/footerStoryRelativeLayout" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"> 
      <TextView 
       android:id="@+id/PageCount" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentBottom="true" 
       android:layout_centerHorizontal="true" 
       android:text="page count"/> 
      <TextView 
       android:id="@+id/textView1" 
       android:layout_width="wrap_content" 
       android:layout_height="fill_parent" 
       android:layout_alignParentTop="true" 
       android:onClick="addBookmark" 
       android:clickable="true"/> 

     </RelativeLayout> 

</LinearLayout> 

我加載在上面的onCreate在textviews一些文本。到現在爲止還挺好。

現在我overiding onConfigurationChanged(),以便當用戶改變屏幕對準我的內容仍然是相同的,的onCreate()不叫重新加載textviews內容。在onConfigurationChanged()中,我還使用下面的代碼來改變我的relativelayout「titleStoryRelativeLayout」的權重。

//change the weight of the textview when screen alignment changes 
    LayoutInflater inflater = LayoutInflater.from(this); 
    LinearLayout ll = (LinearLayout)inflater.inflate(R.layout.activity_story1, null); 
    LinearLayout yourRL = (LinearLayout)ll.findViewById(R.id.footerStoryLinearLayout); 
    yourRL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.1f)); 

然而,一旦我重新調整我的屏幕(那onConfigurationChanged時()被調用)重量的變化似乎並沒有生效。我可以看到變量yourRL等正在填充。當排列的變化,我想我的titleStory TextView的改變(增加/減少)更好的視覺效果的高度 我改變我的RelativeLayout(容納titleStory的TextView)的重量基本原因是監守。通過上面的代碼我已經將ConfigChanged()放在了上面,我期望重量被改變(從0.05到0.01),因此textview的高度也應該改變。

回答

0

由於此XML文件不給錯誤,這並不意味着你可以使用這些屬性(weightorientation和相關聯的LinearLayout你的情況weight)與RelativeLayout的。請首先參閱RelativeLayout的文檔。在那你不會找到android:layout_weight財產,在LinearLayout你可以找到該屬性在Android的文檔。

不過,如果你想用你的重量在RelativeLayout的使用的LinearLayout與fill_parrent高度和寬度。

我猜你讓我..

+0

我做了一些更改,將權重分配給LinearLayout。還是同樣的問題。這裏是我的新XML現在 – user1938357

+0

請參考我最近編輯更新的XML /代碼的問題。 – user1938357

+0

解決它...使用並不需要下面氣筒中的代碼,我coukld直接將Lineralyout .........的LinearLayout yourRL =(的LinearLayout)findViewById(R.id.footerStoryLinearLayout); \t yourRL.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0,0.5F)); – user1938357

2

更新我的XML使用的LinearLayout(問題與新的XML更新),然後用下面的代碼更新的權重。我想通過定義太多的佈局使我變得複雜。

LinearLayout yourRL = (LinearLayout)findViewById(R.id.footerStoryLinearLayout); 
    yourRL.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 0, 0.5f));