2017-01-03 53 views
0

背景:我做的是不僅會造成超時報警基礎,基於位置的應用程序或兩者在創建該警報的活動我複選框各個---- timepicker和地圖,並取消選中如果你不想在你的鬧鐘中使用這些功能之一。這是本活動的圖片 - http://prntscr.com/dr1s74一些RelativeLayout.LayoutParams不起作用

如果取消選中複選框地圖它刪除片段,並設置radioGroup中的佈局PARAMS下面的標題(因爲地圖現在已經不復存在了),中心並增加保證金。

雖然addRule(RelativeLayout.BELOW,id)有效,但setMargins(left,top,right,bottom)和addRule(RelativeLayout.CENTER_HORIZONTAL)不起作用,並且因此radioGroup偏離中心且沒有正確標題下面的空格。

XML活動:

可能出現的問題描述:我能想到它發生的唯一原因是,我有CoordinatorLayout作爲父佈局(因爲FAB和小吃吧之間的相互作用)含有的RelativeLayout(裏面和滾動型如果你有更好的方法,請打開建議)。而且出於某種原因,在嘗試向佈局參數添加規則時會出現問題。 XML代碼(去掉了單選按鈕和複選框以方便查看):

<android.support.design.widget.FloatingActionButton 
    android:id="@+id/saveAlarm" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|right" 
    android:layout_marginBottom="@dimen/fab_margin" 
    android:layout_marginRight="@dimen/fab_margin" 
    android:onClick="fabClicked" 
    app:srcCompat="@android:drawable/ic_menu_save" /> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/relative_layout_alarm_creator" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.example.rome.locationalarm.AlarmCreator"> 

     <EditText 
      android:id="@+id/alarmTitle" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:fontFamily="sans-serif" 
      android:hint="Alarm title" /> 

     <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/map" 
      android:name="com.google.android.gms.maps.SupportMapFragment" 
      android:layout_width="match_parent" 
      android:layout_height="150dp" 
      android:layout_below="@+id/alarmTitle" 
      android:layout_marginTop="10dp" 
      tools:context="com.example.rome.locationalarm.MapsFragment" /> 

     <com.example.rome.locationalarm.CustomTimePicker 
      android:id="@+id/timePicker" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentStart="true" 
      android:layout_below="@+id/days" 
      android:layout_marginTop="10dp" /> 


     <TextView 
      android:id="@+id/chooseLocation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignTop="@+id/map" 
      android:layout_centerHorizontal="true" 
      android:text="Choose Location" /> 


     <RadioGroup 
      android:id="@+id/days" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/map" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="10dp" 
      android:orientation="horizontal"> 

     </RadioGroup> 

    </RelativeLayout> 
</ScrollView> 

Java代碼(只有重要組成部分):

if(locationCheckBox.isChecked()){ 
       //If user wants location 
       RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
       layoutParams.addRule(RelativeLayout.BELOW, mapFragment.getId()); 
       //All of these don't work 
       layoutParams.setMargins(0, 10, 0, 0); // (left, top, right, bottom) 
       //All of these are tries to center the radioGroup 
       layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 
       layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START); 
       layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); 
       layout.removeView(radioGroup); 
       layout.addView(radioGroup, layoutParams); 
       //layout.updateViewLayout(radioGroup, layoutParams); doesn't work as well. 

感謝您抽出時間來嘗試幫助,祝你有個美好的一年!

回答

0

你需要這樣做來獲得的LayoutParams

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams(); 
+0

嘗試調用'requestLayout()'進行更改後。還你應該使用'getLayoutParams()'爲@Tulsi建議,並調整,而不是添加/刪除視圖。也可以嘗試使用'GONE'來改變視野。此外,'CENTER_IN_PARENT'將覆蓋任何其他佈局規則。在任何情況下它是'requestLayout()'觸發以調整其子女(在任何'View')。 –

+0

謝謝你們倆!有用!和@ escape-llc requestLayout()做了什麼? –

+0

這是一個「信號」到'View'其佈局是無效的,並在接下來的佈局傳遞,它應該重新計算其孩子的位置。 UI線程控制何時發生這種情況(即它有一個消息泵);所有的'requestXXX'方法相同的方式工作,通過發佈消息到UI線程執行「後來」。這也是爲什麼在View的回調中操作'View'層次結構是不明智的。 –