关于SB的Layout优先级(priority)

本文详细介绍了如何根据车牌号码的长度动态调整UI布局,包括显示省份简写、触发查询按钮位置变化等逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图

这里写图片描述
1.当车牌号码中文字长度大于0时,显示左边的label
2.当车牌好码中文字长度大于5时,显示下面的view,以及改变查询按钮的位置

思路:

1.设置车牌号码输入框分别于左侧“车牌号码”以及“豫”的约束,分别设置约束优先级,并且连线。先让“豫”隐藏。当文字长度大于1时,让文本输入框与“豫”的优先级最大
2.同理,分别设置查询按钮与上面的优先级,监听文本长度,设置优先级,具体看代码
这里写图片描述

//车牌号码
@property (weak, nonatomic) IBOutlet UITextField *carNumber;
//省份简写
@property (weak, nonatomic) IBOutlet UILabel *shortP;
//下面第二个view
@property (weak, nonatomic) IBOutlet UIView *View2;
//查询按钮与view1的距离约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutWithV1;
//查询按钮与view2的距离约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutWithV2;
//与车牌号的距离约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutWithCarNum;
//与省份简写的距离约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *layoutWithShortP;


@end

@implementation YCBreakViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.carNumber.delegate = self;
    self.layoutWithV1.priority = 333;

    self.layoutWithCarNum.priority = 333;
    [self.carNumber addTarget:self action:@selector(textFieldDidChange:)  forControlEvents:UIControlEventEditingChanged];
}

#pragma mark 文本编辑监听
- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField.text.lastPathComponent > 0) {
        self.shortP.hidden = NO;
        self.layoutWithShortP.priority = 444;
    }
    if (textField.text.length >= 6) {
        self.View2.hidden = NO;
        self.layoutWithV2.priority = 444;
    }
}
package com.example.kucun2.ui.dingdan;//package com.example.kucun2; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.annotation.SuppressLint; import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.HorizontalScrollView; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import androidx.core.content.ContextCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.RecyclerView; import com.example.kucun2.R; import com.example.kucun2.View.HorizontalScrollTextView; import com.example.kucun2.entity.Bancai; import com.example.kucun2.entity.Chanpin; import com.example.kucun2.entity.Chanpin_Zujian; import com.example.kucun2.entity.Dingdan; import com.example.kucun2.entity.Dingdan_Bancai; import com.example.kucun2.entity.Dingdan_Chanpin; import com.example.kucun2.entity.data.Data; import com.example.kucun2.entity.Zujian; import java.util.List; public class OrderDisplayFragment extends Fragment { private TableLayout table; private HorizontalScrollView horizontalScrollView; private ValueAnimator scrollIndicatorAnimator; private boolean isIndicatorVisible = false; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_order_display, container, false); table = view.findViewById(R.id.orderTable); horizontalScrollView = view.findViewById(R.id.horizontalScrollContainer); View scrollIndicator = view.findViewById(R.id.scroll_indicator); // 添加表头 addTableHeader(table); // 填充表格数据 fillTableData(); // 添加滚动监听 horizontalScrollView.getViewTreeObserver().addOnScrollChangedListener(() -> { int maxScroll = horizontalScrollView.getChildAt(0).getWidth() - horizontalScrollView.getWidth(); int currentScroll = horizontalScrollView.getScrollX(); if (currentScroll > 0 && maxScroll > 0) { if (!isIndicatorVisible) { showScrollIndicator(); } // 更新滚动指示器位置 updateScrollIndicatorPosition(currentScroll, maxScroll); } else { hideScrollIndicator(); } }); return view; } private void fillTableData() { List<Dingdan> orders = Data.dingdans; List<Dingdan_Chanpin> orderProducts = Data.dingdanChanpins; List<Dingdan_Bancai> orderMaterials = Data.dingdanBancais; for (Dingdan order : orders) { for (Dingdan_Chanpin orderProduct : orderProducts) { if (orderProduct.getDingdan().getId().equals(order.getId())) { Chanpin product = orderProduct.getChanpin(); for (Chanpin_Zujian component : product.getZujians()) { for (Dingdan_Bancai material : orderMaterials) { if (material.getZujian() != null && material.getZujian().getId().equals(component.getId())) { addTableRow(createRowData( order, product, component, material )); } } } } } } } private void addTableRow(Object[] rowData) { TableRow row = new TableRow(requireContext()); TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT ); row.setLayoutParams(rowParams); row.setMinimumHeight(dpToPx(36)); for (Object data : rowData) { HorizontalScrollTextView textView = new HorizontalScrollTextView(requireContext()); textView.setText(String.valueOf(data)); textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14); int padding = dpToPx(8); textView.setPadding(padding, padding / 2, padding, padding / 2); textView.setMinWidth(dpToPx(50)); TableRow.LayoutParams colParams=null; // 设置背景边框 textView.setBackgroundResource(R.drawable.cell_border); if ( data.toString().length() > 10){ colParams = new TableRow.LayoutParams( 0, // 宽度将由权重控制 TableRow.LayoutParams.MATCH_PARENT, 2.0f ); colParams.weight = 1; }else{ colParams = new TableRow.LayoutParams( 0, // 宽度将由权重控制 TableRow.LayoutParams.MATCH_PARENT, 1.0f ); colParams.weight = 1; } textView.setLayoutParams(colParams); row.addView(textView); } table.addView(row); } // 动态添加表头 (使用自定义TextView) private void addTableHeader(TableLayout table) { TableRow headerRow = new TableRow(requireContext()); headerRow.setLayoutParams(new TableLayout.LayoutParams( TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT )); // 设置行背景颜色 headerRow.setBackgroundColor(ContextCompat.getColor(requireContext(), R.color.purple_500)); // 定义表头 String[] headers = getResources().getStringArray(R.array.table_headers); float[] weights = {1.0f, 1.0f, 1.0f, 1.0f, 2.0f, 1.0f, 1.0f}; // 列宽优先级数组(板材信息列优先) boolean[] priority = {false, false, false, false, true, false, false}; for (int i = 0; i < headers.length; i++) { HorizontalScrollTextView headerView = new HorizontalScrollTextView(requireContext()); headerView.setText(headers[i]); headerView.setTextColor(Color.WHITE); headerView.setTypeface(null, Typeface.BOLD); headerView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); headerView.setPadding(dpToPx(8), dpToPx(8), dpToPx(8), dpToPx(8)); // 为优先级高的列设置最小宽度 if (priority[i]) { headerView.setMinWidth(dpToPx(100)); headerView.weight=2; } // 设置布局参数 TableRow.LayoutParams colParams = new TableRow.LayoutParams( priority[i] ? TableRow.LayoutParams.WRAP_CONTENT : 0, TableRow.LayoutParams.MATCH_PARENT, priority[i] ? 0 : weights[i] // 优先级列不使用权重 ); headerView.setLayoutParams(colParams); headerRow.addView(headerView); } table.addView(headerRow); } private void showScrollIndicator() { isIndicatorVisible = true; View indicator = getView().findViewById(R.id.scroll_indicator); if (scrollIndicatorAnimator != null && scrollIndicatorAnimator.isRunning()) { scrollIndicatorAnimator.cancel(); } indicator.setVisibility(View.VISIBLE); indicator.setAlpha(0f); scrollIndicatorAnimator = ObjectAnimator.ofFloat(indicator, "alpha", 0f, 0.8f); scrollIndicatorAnimator.setDuration(300); scrollIndicatorAnimator.start(); } private void hideScrollIndicator() { isIndicatorVisible = false; View indicator = getView().findViewById(R.id.scroll_indicator); if (scrollIndicatorAnimator != null && scrollIndicatorAnimator.isRunning()) { scrollIndicatorAnimator.cancel(); } scrollIndicatorAnimator = ObjectAnimator.ofFloat(indicator, "alpha", indicator.getAlpha(), 0f); scrollIndicatorAnimator.setDuration(300); scrollIndicatorAnimator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { indicator.setVisibility(View.INVISIBLE); } }); scrollIndicatorAnimator.start(); } private void updateScrollIndicatorPosition(int currentScroll, int maxScroll) { View indicator = getView().findViewById(R.id.scroll_indicator); FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) indicator.getLayoutParams(); // 计算指示器位置(0-100%) float percentage = (float) currentScroll / maxScroll; int maxMargin = getResources().getDisplayMetrics().widthPixels - indicator.getWidth(); // 设置右边距(控制位置) params.rightMargin = (int) (maxMargin * percentage); indicator.setLayoutParams(params); } // DP转PX工具方法 private int dpToPx(int dp) { return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics() ); } /** * 数据组合 * @param order * @param product * @param component * @param material * @return */ private Object[] createRowData(Dingdan order, Chanpin product, Chanpin_Zujian component, Dingdan_Bancai material) { Bancai board = material.getBancai(); String boardInfo = board.TableText(); ; return new Object[] { order.getNumber(), // 订单号 product.getId(), // 产品编号 "1", // 产品数量 (根据需求调整) component.getZujian().getName(), // 组件名 boardInfo, // 板材信息 Math.round(component.getOne_several()), // 板材/组件 material.getShuliang() // 订购数量 }; } } colParams.weight = 1;管用 但标题错位
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值