2017-01-03 7 views
0

背景:タイムベースのアラーム、ロケーションベース、またはその両方を作成できるアプリケーションを作成しています。アラームを作成するアクティビティには、タイムピッカーとマップのチェックボックスがあります。あなたのアラームにこれらの機能の1つを必要としない場合は、それは。この活動の写真は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()'を使用し、ビューの追加/削除の代わりに調整する必要があります。また、そのビューを 'GONE'にして、可視性を変更してみてください。 'CENTER_IN_PARENT'は他のレイアウトルールよりも優先されます。いずれにしても、viewLayout()はビューの子を調整する( 'View')。 –

+0

ありがとう皆さん!できます! @ escape-llc requestLayout()は何をしますか? –

+0

は、そのレイアウトが無効であることを 'View 'への「信号」であり、次のレイアウトのパスでは、子の位置を再計算する必要があります。 UIスレッドは、これがいつ発生するのかを制御する(すなわち、メッセージポンプを有する)。すべての 'requestXXX'メソッドは、実行のためにUIスレッドに"後で "メッセージをポストすることによって、同じ方法で動作します。これは 'View'のコールバックから' View'階層を操作することが賢明でない理由です。 –

関連する問題