相對佈局設置位置

有的時候有寫佈局總是非常相似或者只有一兩個控件不一樣,這個時候我們往往會考慮重複使用這個佈局。如下圖:
這裏寫圖片描述

一個是秒殺的頁面,一個是做返券的頁面,兩者的佈局基本一樣。比如我將價格跟倒計時放到一個相對佈局裏面,那麼這時候就要根據不同的頁面動態設置位置。(佈局裏面倒計時不能直接設置相對價格的右邊,不然動態設置代碼的時候無效)
佈局代碼:

              <RelativeLayout
                android:layout_marginTop="15px"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_centerVertical="true"
                    android:id="@+id/tvPrice"
                    android:layout_gravity="center_vertical"
                    android:text="¥180.00"
                    android:textColor="@color/red"
                    android:textSize="@dimen/font_common_size_big"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
                <com.lianjiu.b.common.widget.CountDownTextView
                    android:layout_centerVertical="true"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="20px"
                    android:id="@+id/countDownTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">        </com.lianjiu.b.common.widget.CountDownTextView>
</RelativeLayout>

秒殺Adapter裏面動態設置控件的位置(倒計時在價格的右邊):

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) countDownTextView.getLayoutParams();
ayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.addRule(RelativeLayout.RIGHT_OF,viewHolder.tvPrice.getId());
countDownTextView.setLayoutParams(layoutParams);

返券Adapter裏面動態設置控件的位置(倒計時有頁面最右側)

 RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)   countDownTextView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
countDownTextView.setLayoutParams(layoutParams);

這樣就能實現所需要的效果了。

發佈了38 篇原創文章 · 獲贊 17 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章