2016-03-30 1 views
1

У меня проблема с моим приложением для Android. Я устанавливаю ширину трех видов с тем же размером, что и три кнопки над представлениями. Представления - это индикаторы и показывают, какая кнопка нажата. Может быть, не самое умное решение, но я просто стажер ...Просмотров не будут выравниваться прямо в RelativeLayout (ширина от представления, установленного в java-коде)

Кодекс от моего XML является:

<RelativeLayout 
     android:id="@+id/statusIndicatorTop" 
     android:layout_width="wrap_content" 
     android:layout_height="@dimen/status_indicator_height" 
     android:background="@color/colorPrimaryDark" 
     android:layout_below="@+id/toolBar" 
     android:orientation="horizontal" 
     android:gravity="right"> 

     <View 
      android:id="@+id/statusPreise" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignLeft="@id/statusShops" /> 

     <View 
      android:id="@+id/statusShops" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignLeft="@id/statusKategorie"/> 

     <View 
      android:id="@+id/statusKategorie" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignParentRight="true" /> 

    </RelativeLayout> 

Моего JAVA код в OnCreate является:

@Override 
    public void onStart() { 
     super.onStart(); 

     fragmentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

      @Override 
      public void onGlobalLayout() { 

       fragmentView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 

       Button btnKat = (Button) fragmentView.findViewById(R.id.button_Kategorie); 
       int btnKatWidth = btnKat.getWidth(); 
       RelativeLayout.LayoutParams paramsKat = (RelativeLayout.LayoutParams) statusKategorie.getLayoutParams(); 
       paramsKat.width = btnKatWidth; 
       statusKategorie.setLayoutParams(paramsKat); 

       Button btnShops = (Button) fragmentView.findViewById(R.id.button_Shops); 
       int btnShopsWidth = btnShops.getWidth(); 
       RelativeLayout.LayoutParams paramsShops = (RelativeLayout.LayoutParams) statusShops.getLayoutParams(); 
       paramsShops.width = btnShopsWidth; 
       statusShops.setLayoutParams(paramsShops); 

       Button btnPreise = (Button) fragmentView.findViewById(R.id.button_Search); 
       int btnPreiseWidth = btnPreise.getWidth(); 
       RelativeLayout.LayoutParams paramsPreise = (RelativeLayout.LayoutParams) statusSuche.getLayoutParams(); 
       paramsPreise.width = btnPreiseWidth; 
       statusSuche.setLayoutParams(paramsPreise); 
      } 
     }); 
    } 

В LOG я получаю это сообщение об ошибке:

Error:(185, 39) No resource found that matches the given name (at 'layout_alignLeft' with value '@id/statusShops'). 
Error:(193, 39) No resource found that matches the given name (at 'layout_alignLeft' with value '@id/statusKategorie'). 

Когда я не использую AlignLeft, на первом и втором зрения, мое приложение начинается и третий индикатор делает то, что я хочу. Я могу представить, почему приложение не работает, но я не могу исправить это из-за моего отсутствия опыта. Надеюсь, этого достаточно, чтобы решить мою проблему.

Заранее спасибо.

ответ

1

Порядок это важно, вы не можете обратиться к тем, что не было обращено еще (т.е. вы не можете сделать, если [email protected]/statusKategorie statusKategory не было обращено все же). Попытайтесь изменить порядок своих объявлений в xml, например, так:

<RelativeLayout 
    android:id="@+id/statusIndicatorTop" 
    android:layout_width="wrap_content" 
    android:layout_height="@dimen/status_indicator_height" 
    android:background="@color/colorPrimaryDark" 
    android:layout_below="@+id/toolBar" 
    android:orientation="horizontal" 
    android:gravity="right"> 

    <View 
     android:id="@+id/statusKategorie" 
     android:layout_width="0dp" 
     android:layout_height="@dimen/status_indicator_height" 
     android:background="@color/colorAccent" 
     android:visibility="invisible" 
     android:layout_alignParentRight="true" /> 
    <View 
     android:id="@+id/statusShops" 
     android:layout_width="0dp" 
     android:layout_height="@dimen/status_indicator_height" 
     android:background="@color/colorAccent" 
     android:visibility="invisible" 
     android:layout_alignLeft="@id/statusKategorie"/> 
    <View 
     android:id="@+id/statusPreise" 
     android:layout_width="0dp" 
     android:layout_height="@dimen/status_indicator_height" 
     android:background="@color/colorAccent" 
     android:visibility="invisible" 
     android:layout_alignLeft="@id/statusShops" /> 
</RelativeLayout> 
+0

Спасибо, что это работает. Мне нужно немного изменить свою Java, но я думаю, что это работает как шарм. –

0

Попробуйте XML:

<RelativeLayout 
     android:id="@+id/statusIndicatorTop" 
     android:layout_width="wrap_content" 
     android:layout_height="@dimen/status_indicator_height" 
     android:background="@color/colorPrimaryDark" 
     android:layout_below="@+id/toolBar" 
     android:orientation="horizontal" 
     android:gravity="right"> 

     <View 
      android:id="@+id/statusPreise" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignLeft="@id/statusShops" /> 

     <View 
      android:id="@+id/statusKategorie" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignParentRight="true" /> 

     <View 
      android:id="@+id/statusShops" 
      android:layout_width="0dp" 
      android:layout_height="@dimen/status_indicator_height" 
      android:background="@color/colorAccent" 
      android:visibility="invisible" 
      android:layout_alignLeft="@id/statusKategorie"/> 

    </RelativeLayout>