android 使用TableLayout 实现布局自动拉伸宽度 LinearLayout中元素按比例分配宽度

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);  

猜你喜欢

转载自blog.csdn.net/qq_36135335/article/details/82982158