2017-01-30 62 views
0

查看創建當我嘗試以編程方式添加RelativeLayout並直接使用xml視圖時,存在差異。視圖使用xml創建的差異 - 使用代碼

如果我複製粘貼在它的工作原理container_tabs幾個RelativeLayout的..

,當我嘗試誇大一個RelativeLayout的文件(完全相同作爲進入container_tabs)的結果是不同的,它不工作。

我如何填寫我的看法編程

@BindView(R.id.container_tabs) 
    protected LinearLayout mLinearLayoutContainerTabs; 

    private List<CustomBookingTab> mBookingTabsList = new ArrayList<>(); 

    private void initCustomButtonsTabs() { 
     mBookingTabsList.add(new CustomBookingTab(this, EnumBookingTab.BOOKING_TAB_CALENDAR, R.drawable.ic_calendar_white)); 
     mBookingTabsList.add(new CustomBookingTab(this, EnumBookingTab.BOOKING_TAB_TIME, R.drawable.ic_clock)); 


     for (int i = 0; i < mBookingTabsList.size(); i++) { 
      mLinearLayoutContainerTabs.addView(mBookingTabsList.get(i).getRelativeLayoutBookingTab(), i); 
     } 
    } 

我如何從一個XML文件中誇大我的觀點(CustomBookingTab構造函數)

mRelativeLayoutBookingTab = (RelativeLayout) inflater.inflate(R.layout.booking_tab, null); 

主視圖

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="-29dp" 
     android:orientation="horizontal"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:id="@+id/container_tabs" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <!-- Tab item --> 
       <RelativeLayout 
        android:layout_width="0dp" 
        android:layout_height="70dp" 
        android:layout_gravity="center" 
        android:layout_weight="1"> 

        <FrameLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <ImageView 
          android:layout_width="70dp" 
          android:layout_height="70dp" 
          android:layout_alignParentLeft="true" 
          android:background="@drawable/circle_blue_button_border_white" 
          android:padding="21dp" 
          android:src="@drawable/ic_calendar_white" 
          android:text="Button" 
          android:textColor="#FFFFFF" 
          android:textSize="30sp" /> 

        </FrameLayout> 

       </RelativeLayout> 


      </LinearLayout> 

     </LinearLayout> 

    </LinearLayout> 

有沒有一個很好的方法來做到這一點?哪裏不對?

+0

「mLinearLayoutContainerTabs」在哪裏創建?你能提供更多的代碼嗎? –

+0

我加了'mLinearLayoutContainerTabs'。這只是一個簡單的活動 – Johnny

回答

0

從XML中添加視圖和通過解剖學膨脹視圖有一些區別。

但是如果您在準備XML時使用相對佈局,則會使用如此多的相關屬性將不同視圖一起設置爲XML。

而當我們使用動態相對佈局我們應該用ADDRULE的選項像波紋管添加視圖:

RelativeLayout relativeLayout = new RelativeLayout(getActivity()); 
     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 
     Button button1 = new Button(this); 
     button1.setId(1); 
     Button button2 = new Button(this); 
     button2.setId(2); 
     relativeLayout.addView(button1); 
     relativeLayout.addView(button2); 
     RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) button1.getLayoutParams(); 
     lp.addRule(RelativeLayout.RIGHT_OF, button2.getId()); 
     lp.addRule(RelativeLayout.LEFT_OF, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_END, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_LEFT, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_BASELINE, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, button2.getId()); 
     lp.addRule(RelativeLayout.ABOVE, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_TOP, button2.getId()); 
     lp.addRule(RelativeLayout.START_OF, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_RIGHT, button2.getId()); 
     lp.addRule(RelativeLayout.ALIGN_PARENT_END, button2.getId()); 
     lp.addRule(RelativeLayout.END_OF, button2.getId()); 

幾乎所有相對佈局屬性可以通過動態AddRull。