android 使用TableLayout 實現佈局自動拉伸寬度 LinearLayout中元素按比例分配寬度
阿新 • • 發佈:2018-11-07
TableLayout 實現佈局自動拉伸寬度
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
</TableLayout>
設定android:stretchColumns="*"或者根據需要設定
如:android:stretchColumns="0,1,2,3" 設定四個表格都根據需要拉伸寬度。
LinearLayout中元素按比例分配寬度
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:text="時區" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.5" android:gravity="center" android:padding="5dp" android:textColor="@color/white"/> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/split_line2" /> <TextView android:text="序號" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/white" /> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/split_line2" /> <TextView android:text="人員" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/white" /> <TextView android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/split_line2" /> <TextView android:text="數量" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:padding="5dp" android:textColor="@color/white" /> </LinearLayout>
時區設定權重android:layout_weight="1.5"為1.5其他三個為1下圖看效果。
動態設定LinearLayout中元素按比例分配寬度
/** * 發車資料動態新增狀態 */ private void showData(List<CheckedFcRecord> titleData) { for (int i = 0; i < titleData.size(); i++) { final CheckedFcRecord pojo = titleData.get(i); LinearLayout llWashingRoomItem = new LinearLayout(view.getContext()); llWashingRoomItem.setLayoutParams(new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); llWashingRoomItem = (LinearLayout) getActivity().getLayoutInflater().inflate(R.layout.checkedfcrecord_template, null); TextView time = (TextView) llWashingRoomItem.findViewById(R.id.time); TextView vhclNo = (TextView) llWashingRoomItem.findViewById(R.id.vhclNo); TextView jpy = (TextView) llWashingRoomItem.findViewById(R.id.jpy); TextView ticket = (TextView) llWashingRoomItem.findViewById(R.id.ticket); time.setText(DateTools.getStringFromDate(pojo.getFcTime(),null)); vhclNo.setText(pojo.getVhcl_no()); jpy.setText(pojo.getJsy_name()); //Integer型別需要轉換用.toString()不然報錯 ticket.setText(pojo.getJps().toString()); //動態設定layout_weight權重設定表格寬度 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f); time.setLayoutParams(lp); lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); vhclNo.setLayoutParams(lp); lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); jpy.setLayoutParams(lp); lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f); ticket.setLayoutParams(lp); wr_areas.addView(llWashingRoomItem); } }
主要就這一段程式碼
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT, 1.5f);
time.setLayoutParams(lp);