2017-01-03 7 views
0

배경 : 시간 기반 알람, 위치 기반 또는 둘 다를 기반으로 알람을 생성하는 응용 프로그램을 만들고 있습니다. 각 timepicker 및 맵에 대한 확인란이 있으며 선택을 취소합니다. 알람 기능 중 하나를 원하지 않는다면 이 활동에 대한 그림은 http://prntscr.com/dr1s74입니다.일부 RelativeLayout.LayoutParams가 작동하지 않습니다.

지도 확인란을 선택 취소하면 조각을 제거하고 라디오 그룹의 레이아웃 매개 변수를 제목 아래로 설정합니다 (지도가 사라짐). 중앙에 배치하고 여백을 추가합니다.

addRule (RelativeLayout.BELOW, id)가 작동하지만 setMargins (left, top, right, bottom) 및 addRule (RelativeLayout.CENTER_HORIZONTAL)이 작동하지 않으며 radioGroup이 중앙에서 벗어나 올바른 위치에 있지 않기 때문에 제목 아래 공간.

XML 작업 : 가능한 문제의

설명 : 나는 그런 일이 생각할 수있는 유일한 이유는 내가 그 안에와있는 ScrollView (FAB 및 스낵바 사이의 상호 작용이 있기 때문에) RelativeLayout의를 (포함 된 상위 레이아웃으로 CoordinatorLayout을 가지고있다 더 나은 방법이 있다면 제안에 개방하십시오.) 레이아웃 매개 변수에 규칙을 추가 할 때 어떤 이유로 문제가 발생합니다. 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()'변경 한 후에. @Tulsi가 제안한대로'getLayoutParams()'를 사용해야하고,보기 추가/제거 대신 조정해야합니다. 또한보기를 '사라짐'으로 설정하고 가시성을 변경하십시오. 또한 CENTER_IN_PARENT는 다른 레이아웃 규칙보다 우선합니다. 어쨌든'viewLayout()'은 뷰를 트리거하여 뷰의 자식을 조정한다. –

+0

둘 다 감사합니다! 그것은 작동합니다! @ escape-llc requestLayout()은 무엇을합니까? –

+0

은 레이아웃에 유효하지 않은'View '에 대한 "신호"이며, 다음 레이아웃 패스에서 그 아이의 위치를 ​​다시 계산해야합니다. UI 스레드는 이것이 언제 일어나는지를 제어한다 (즉, 메시지 펌프를 갖는다). 모든 requestXXX 메소드는 UI 스레드에 메시지를 게시하여 "나중에"실행하도록 동일한 방식으로 작동합니다. 이것은 또한'View' 콜백 내에서'View' 계층 구조를 조작하는 것이 현명하지 않은 이유이기도합니다. –