LinearLayout之初级学习 (二) ScrollView

上一篇文章 

MyLinearLayout 之 初级学习经验
介绍了,关于UILabel 的自动布局 

这一片文章主要介UIScrollView 的自动布局

第一步为UIScrollView 初始化,

    UIScrollView *scrollView = [UIScrollView new];
    scrollView.backgroundColor = [UIColor whiteColor];
    self.view = scrollView ;
    MyLinearLayout *linearLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    linearLayout.wrapContentHeight = YES ;
    linearLayout.myLeftMargin = linearLayout.myRightMargin = 0 ;
    linearLayout.padding = UIEdgeInsetsMake(10, 10, 10, 10) ;
    [self.view addSubview:linearLayout];


(1)在ScrollView 中添加一个Vertical 形式的一个子布局

  效果如下


 分析:  1. 子视图类型 UILabel 和 UITextField

              2. 排列方式 竖直排列 (Vertical) 对应MyLinearLayout 的类型是 MyLayoutViewOrientation_Vert

              3.  为Label 和TextField 添加一个共同的layout,这样比较好管理


  代码实现:

先新建一个共同的Layout 如下

    MyLinearLayout *vertical1 = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert];
    vertical1.myMargin = 0 ;
    vertical1.wrapContentWidth = NO ;
    vertical1.wrapContentHeight = YES ;
    [layout addSubview:vertical1];


在layout 中添加Label

 UILabel *label = [[UILabel alloc] init];
    label.myMargin = 0 ;
    label.text = @"编号" ;
    label.flexedHeight = YES ;
    [label sizeToFit] ;
    [vertical1 addSubview:label];

在layout 中添加UITextField


 UITextField *textField = [[UITextField alloc] init];
    textField.borderStyle = UITextBorderStyleRoundedRect ;
    textField.myTopMargin = 10 ;
    textField.myLeftMargin = textField.myRightMargin = 0 ;
    textField.myHeight = 30 ;
    textField.placeholder = @"这里输入编号" ;
    [vertical1 addSubview:textField];


(2)   年龄和下面的整体是一个layout类型为   例如



 分析:1. 三个等间距 等宽的label,横向排列(MyLayoutViewOrientation_Horz)叫暂时称为horizLayout

            2. 年龄 和 horizLayout 纵向排列


          先定义了一个总的Layout


    MyLinearLayout *ageLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Vert]; // 定义纵向排列
    ageLayout.layer.borderColor = [UIColor blackColor].CGColor;  // 定义边线的颜色
    ageLayout.layer.borderWidth = 1 ; // 定义边线的宽度
    ageLayout.layer.cornerRadius = 5 ;  // 定义圆角的大小
    ageLayout.myTopMargin = 10 ; // 定义上边界
    ageLayout.myLeftMargin = ageLayout.myRightMargin = 0 ; // 定义左边界 和 右边界
    ageLayout.wrapContentHeight = YES ;   // 定义约束的高度是自适应的
    ageLayout.padding = UIEdgeInsetsMake(5, 5, 5, 5) ; // 定义约束的内边距


    添加一个labe


    UILabel *age = [[UILabel alloc] init];
    age.text = @"年龄" ;
    [age sizeToFit]; // 自适应宽度
    age.flexedHeight = YES ; // 自适应宽度
    age.myLeftMargin = age.myTopMargin = 0 ; // 定义左边界和 上边界
    [ageLayout addSubview:age];


    在添加一个横向布局的约束用来装三个label

   MyLinearLayout *coLayout = [MyLinearLayout linearLayoutWithOrientation:MyLayoutViewOrientation_Horz];
    coLayout.myTopMargin = 5 ;
    coLayout.subviewMargin = 5 ;
    coLayout.myLeftMargin = coLayout.myRightMargin = 0 ;
    coLayout.wrapContentHeight = YES ;
    [ageLayout addSubview:coLayout];


   添加三个label 设置weight = 1 这样label 的宽度会根据margin 来均分剩余的宽度

  UILabel *l1 = [[UILabel alloc] init];
    l1.text = @"20" ;
    l1.flexedHeight = YES ;
    l1.textAlignment = NSTextAlignmentCenter ;
    l1.weight = 1;
    l1.backgroundColor = [UIColor redColor];
    [coLayout addSubview:l1];
    
    UILabel *l2 = [[UILabel alloc] init];
    l2.text = @"30" ;
    l2.weight = 1;
    l2.textAlignment = NSTextAlignmentCenter ;
    l2.flexedHeight = YES ;
    l2.backgroundColor = [UIColor redColor];
    [coLayout addSubview:l2];

    
    UILabel *l3 = [[UILabel alloc] init];
    l3.text = @"40" ;
    l3.weight = 1 ;
    l3.textAlignment = NSTextAlignmentCenter ;
    l3.flexedHeight = YES ;
    l3.backgroundColor = [UIColor redColor];
    [coLayout addSubview:l3];









 




  








 



 




在Android中,虽然通常我们不会直接将`LinearLayout`作为`ScrollView`的直接子元素,因为`LinearLayout`本身并不支持滚动,而`ScrollView`的主要作用就是容纳可以滚动的内容。但是如果你确实需要这种布局结构,你可以这样做: 1. 首先创建一个新的布局文件,例如`scroll_linear_layout.xml`,在这个文件里设置一个`LinearLayout`作为根视图: ```xml <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/linear_layout_container" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 这里添加你的LinearLayout的子项 --> </LinearLayout> </ScrollView> ``` 2. 然后在你的Activity或Fragment中,找到这个`LinearLayout`并添加内容: ```java LinearLayout linearLayout = findViewById(R.id.linear_layout_container); linearLayout.addView(yourChildView); // 将需要滚动的View添加到LinearLayout中 // 如果有多个子项需要滚动,循环添加即可 List<View> childViews = ...; for (View view : childViews) { linearLayout.addView(view); } ``` 需要注意的是,虽然这样设置,实际操作上可能会因为`LinearLayout`本身的滚动限制而无法完全滚动内部的所有内容,除非你手动处理滚动事件。如果要让内容完全滚动,建议考虑使用其他的滚动容器,如`NestedScrollView`或自定义的滚动组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值