tablayout设置下划线总是自动被设置回去

android LinearLayout滚动条自动显示_百度知道
android LinearLayout滚动条自动显示
怎么能让内容超过屏幕的时候显示出来滚动条~现在的是需要拖拉才可以显示出来一个滚动条~而且我想让LinearLayout内容超过屏幕的时候,可以自动滚到末尾~~
我有更好的答案
这样的功能不适合用LinearLayout来实现,应当使用 ScrollView。追踪最末尾的方法是,每次ScroolView的内容改变时,都执行一下这一句:scrollText.post(new Runnable() {
public void run() {
scrollText.fullScroll(ScrollView.FOCUS_DOWN);
}});这里假设这个ScroolView是scrollText。
采纳率:42%
为您推荐:
其他类似问题
linearlayout的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。【Android】linearlayout点击变换颜色
直接贴代码:
1. layout中的相关代码:
&LinearLayout
android:id="@+id/llGoodCommentContainer"
android:orientation="vertical"
android:layout_weight="1"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/layout_selector"
android:clickable="true"
&ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/zan"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="赞!"
android:textColor="#ffffff"
android:focusable="false"
&/LinearLayout&
2. drawable/文件夹下的layout_selecter.xml文件,这个文件就是设置点击時的颜色和默认显示時的颜色
&?xml version="1.0" encoding="utf-8"?&
&selector xmlns:android="http://schemas.android.com/apk/res/android"&
&item android:state_focused="true" android:drawable="@color/transparent_red"/&
&item android:state_pressed="true" android:drawable="@color/transparent_red" /&
&item android:drawable="@color/red"/&
&/selector&
3. color.xml文件中的两个颜色:
&color name="red"&#ff5654&/color&
&color name="transparent_red"&#ddff5654&/color&最后的说明, 我之前这么弄一直不出效果,后来发现,需要点击效果的Linearlayout的parent控件我设置了@color/red背景,这样的话,点击Linearlayout其实这个LinearLayout是变色的,但是由于@color/transparent_red只是@color/red变换了alpha值的颜色,所以效果被parent控件的颜色覆盖了,显示出来的没有任何效果.
没有更多推荐了,java SWT:自定义布局(Layout)实现组件自动缩放显示
什么是布局(Layout)
窗口布局(Layout)其实是指Composite中组件的一种定位原则的实现,当Composite改变大小时,会自动调用Composite初始化时设置的Layout对象来重新调整所有组件的位置。
一般的UI框架都提供了一些默认布局,比如SWT中的FillLayout,GridLayout…如果使用WindowBuilder开发UI,可以在Design界面下看到所有SWT提供的布局对象,见下图
自定义布局
有的时候,使用SWT提供的布局是无法满足需要的,这种情况下,就需要自实现所需的特殊布局。
实现自定义的Layout并不复杂,
以下是的简要注释说明:
package org.eclipse.swt.
import org.eclipse.swt.graphics.*;
* 布局抽象类,
* 用于控制组件内所有子对象的位置和尺寸
public abstract class Layout {
* 必须实现的抽象方法
* 返回容器组件(父窗口)的Client区域尺寸
protected abstract Point computeSize (Composite composite, int wHint, int hHint, boolean flushCache);
protected boolean flushCache (Control control) {
return false;
* 必须实现的抽象方法
* 设置所有容器组件(父窗口)内所有子组件的位置和大小
* composite 将被重新设置布局的容器组件(父窗口)
* flushCache &code&true&/code& means flush cached layout values
protected abstract void layout (Composite composite, boolean flushCache);
从上面的代码可以知道,只要实现抽象类的两个抽象方法就可以实现一个特殊布局了,SWT提供的那些默认布局类都是通过继承Layout实现的
关于Layout的详细原文说明参见SWT的javadoc
组件自动缩放显示
上一节讲完Layout的实现思路,下面就以以一个实例来说明如何实现自定义布局。
比如下面的图中矩形框,并不是画在背景图上的,而是背景透明的Composite,可以移动和改变尺寸(如何实现,参见我的上一篇博客)
这些矩形用于对图像中的人脸位置进行标注,我们希望当图像大小和位置改变的时候,这些矩形在图像上的相对位置保持不变。
这种需求,SWT中现成的布局都不能满足要求,所以就要自己实现一个,以下是实现代码,
ActiveRectContainer.java
package net.gdface.
import java.net.URL;
import java.util.ArrayL
import java.util.L
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintE
import org.eclipse.swt.events.PaintL
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.I
import org.eclipse.swt.graphics.P
import org.eclipse.swt.graphics.R
import org.eclipse.swt.widgets.C
import org.eclipse.swt.widgets.C
import org.eclipse.swt.widgets.D
import org.eclipse.swt.widgets.L
import org.eclipse.wb.swt.SWTResourceM
* 活动矩形显示容器
* 窗口尺寸改变时所有{@link ActiveRectangle}对象自动等比例改变
* guyadong
public class ActiveRectContainer extends Decorations {
* 创建自定义的布局对象实现窗口内的ActiveRectangle对象能根据父窗口的尺寸改变而同步等比例改变,
* 以保持每一个矩形在父窗口上的相对位置不变
* guyadong
class ZoomLayout extends Layout {
protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Rectangle bounds = composite.getClientArea();
return new Point(bounds.width, bounds.height);
protected void layout(Composite composite, boolean flushCache) {
Control[] childrens = composite.getChildren();
Rectangle originalbounds = getBackgroundImage().getBounds();
Rectangle bounds = composite.getBounds();
float zoomX = (float) bounds.width / originalbounds.
float zoomY = (float) bounds.height / originalbounds.
for (Control children : childrens) {
if (children instanceof ActiveRectangle) {
((ActiveRectangle) children).zoom(zoomX, zoomY);
}else if(children.getLayoutData() instanceof Rectangle){
Rectangle originalBounds=(Rectangle) children.getLayoutData();
children.setBounds(new Rectangle((int)(originalBounds.x*zoomX),(int)(originalBounds.y*zoomY),(int)(originalBounds.width*zoomX),(int)(originalBounds.height*zoomY)));
protected List&ActiveRectangle& annRects=EMPTY_RECTS;
private static final List&ActiveRectangle& EMPTY_RECTS=new ArrayList&ActiveRectangle&();
* image 显示的背景图像,为null时不显示
* rects 显示的矩形对象数组
* focusIndex 焦点矩形索引,超出 rects索引范围时无效
public ActiveRectContainer(Composite parent, Image image, Rectangle[] rects, int focusIndex) {
super(parent, SWT.BORDER|SWT.RESIZE);
if(null!=rects&&rects.length&0){
this.annRects=new ArrayList&ActiveRectangle&();
for(Rectangle rect:rects){
annRects.add(new ActiveRectangle(this, false,rect));
this.annRects.get(focusIndex).focus=true;
}catch(IndexOutOfBoundsException
this.setBackgroundImage(null==image?SWTResourceManager.getMissingImage():image);
this.setLayout(new ZoomLayout());
addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
paintImage(e.gc);
* url 背景图像的URL
* focusIndex
* #AutoZoomRecContainer(Composite, Image, Rectangle[], int)
public ActiveRectContainer(Composite parent, URL url, Rectangle[] rects, int focusIndex) {
this(parent, SWTResourceManager.getImage(url),rects, focusIndex);
* 将 {@link #image} 重绘图像到窗口(缩放到整个窗口)
protected final void paintImage(GC gc) {
boolean isAdvanced = gc.getAdvanced();
gc.setAdvanced(true);
gc.setAntialias(SWT.ON);
Point destSize = getSize();
Image image = getBackgroundImage();
Rectangle imgSize = image.getBounds();
gc.drawImage(image, 0, 0, imgSize.width, imgSize.height, 0, 0, destSize.x, destSize.y);
} finally {
gc.setAdvanced(isAdvanced);
* 以窗口中心为原点对窗口进行缩放
* zoomX x轴缩放比例
* zoomY x轴缩放比例
public void zoomCenter(float zoomX, float zoomY) {
Rectangle originalSize = getBackgroundImage().getBounds();
Rectangle bounds=getBounds();
int width=(int) (originalSize.width*zoomX);
int height=(int) (originalSize.height*zoomX);
int x=bounds.x+(bounds.width-width)/2;
int y=bounds.y+(bounds.height- height)/2;
super.setBounds(x, y, width, height);
* x/y轴等比例缩放
* zoom 缩放比例
* #zoomCenter(float, float)
public void zoomCenter(float zoom) {
zoomCenter(zoom,zoom);
protected void checkSubclass() {
注意:自定义布局实现在ActiveRectContainer.java的代码中是以一个内部类ZoomLayout 来实现的
以下是用WindowBuilder生成的测试代码
TestRectContainer.java
import org.eclipse.swt.widgets.B
import org.eclipse.swt.widgets.D
import org.eclipse.swt.widgets.S
import org.eclipse.wb.swt.SWTResourceM
import org.slf4j.L
import org.slf4j.LoggerF
import net.gdface.ui.ActiveRectC
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.I
import org.eclipse.swt.graphics.P
import org.eclipse.swt.graphics.R
public class TestRectContainer {
static final Logger logger = LoggerFactory.getLogger(TestRectContainer.class);
protected S
* Launch the application.
public static void main(String[] args) {
TestRectContainer window = new TestRectContainer();
window.open();
} catch (Exception e) {
e.printStackTrace();
* Open the window.
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
* 返回适合当前窗口尺寸完整显示图像的缩放比例,图像长宽都小于显示窗口时,则返回1
private static float fitZoom(Point parentSize,Point childrenSize) {
if (childrenSize.x & parentSize.x && childrenSize.y & parentSize.y)
return 1f;
if (childrenSize.x * parentSize.y & childrenSize.y * parentSize.x) {
return ((float) parentSize.y) / (float)childrenSize.y;
return ((float) parentSize.x) / (float)childrenSize.x;
private static Rectangle fitBounds(Point parentSize,Point childrenSize) {
float zoom = fitZoom(parentSize, childrenSize);
childrenSize.x*=
childrenSize.y*=
return new Rectangle((parentSize.x-childrenSize.x)/2,(parentSize.y-childrenSize.y)/2,childrenSize.x,childrenSize.y);
* Create contents of the window.
protected void createContents() {
shell = new Shell();
shell.setText("SWT Application");
shell.setSize(569, 459);
Rectangle[] rects = new Rectangle[]{
new Rectangle(50,50,200,200),
new Rectangle(350,100,150,250),
new Rectangle(125,300,200,321)};
ActiveRectC
Image image = SWTResourceManager.getImage("J:/workspace.neon/iadbui/src/image/.jpg");
Point imgSize=new Point(image.getBounds().width,image.getBounds().height);
canvas = new ActiveRectContainer(shell, image,rects,0);
canvas.setBounds(fitBounds(shell.getSize(),imgSize));
Button btnNewButton = new Button(canvas, SWT.NONE);
btnNewButton.setBounds(189, 95, 80, 27);
btnNewButton.setLayoutData(new Rectangle(189, 95, 80, 27));
btnNewButton.setText("New Button");
对于一般的Control对象,如果没有通过setLayoutData方法设置原始的尺寸位置,则Layout对其无效,所以上面的测试代码中对btnNewButton调用了setLayoutData,指定了初始的位置和尺寸。这样它才能与父窗口同步缩放。如下图
如果注释掉 btnNewButton.setLayoutData(new Rectangle(189, 95, 80, 27));这一行,效果是这样的
没有更多推荐了,iOS 8 Auto Layout界面自动布局系列3-使用代码添加布局约束
本系列的第一篇文章介绍了自动布局的基本原理,第二篇文章通过一个简单的例子演示了如何使用Xcode的Interface Builder(简称IB)以可视化方式添加约束。本篇为该系列的第三篇文章,主要介绍如何通过代码来添加布局约束。
其实,我个人认为本篇才应该是系列的第二篇,因为通过代码构建自动布局约束是最基础的,也是最灵活的方式。而IB只不过是把复杂的过程以直观简单的方式呈现出来,而且并非所有的情况都能用IB来解决,所以学习并掌握通过代码来添加自动布局约束是非常必要的。但是为了降低学习的难度,提高读者理解和接受的程度,最终还是把本编放在第三的位置。闲言少叙,我们进入正题。
第一篇文章中讲到,每一个布局约束就是一个明确的线性变化规则,在数学上是以一次函数的形式表示,即:
y = m * x + c   (公式3.1)
在UIKit中,每一个布局约束是一个NSLayoutConstraint实例,NSLayoutConstraint类的主要属性定义如下:
NS_CLASS_AVAILABLE_(6_0)
@interface NSLayoutConstraint : NSObject
@property (readonly, assign) id firstI
@property (readonly) NSLayoutAttribute firstA
@property (readonly) NSLayoutR
@property (readonly, assign) id secondI
@property (readonly) NSLayoutAttribute secondA
@property (readonly) CGF
@property CGF
+(instancetype)constraintWithItem:(id)firstItem attribute:(NSLayoutAttribute)firstAttribute
relatedBy:(NSLayoutRelation)relation
toItem:(id)secondItem attribute:(NSLayoutAttribute)secondAttribute
multiplier:(CGFloat)multiplier constant:(CGFloat)
其中的firstItem与secondItem分别是界面中受约束的视图与被参照的视图。他们不一定非得是兄弟关系或者父子关系,只要是他们有着共同的祖先视图即可,这一点是autoresizingMask无法做到的。
firstAttribute与secondAttribute分别是firstItem与secondItem的某个布局属性(NSLayoutAttribute):
typedef NS_ENUM(NSInteger, NSLayoutAttribute)
NSLayoutAttributeLeft = 1,
NSLayoutAttributeRight,
NSLayoutAttributeTop,
NSLayoutAttributeBottom,
NSLayoutAttributeLeading,
NSLayoutAttributeTrailing,
NSLayoutAttributeWidth,
NSLayoutAttributeHeight,
NSLayoutAttributeCenterX,
NSLayoutAttributeCenterY,
NSLayoutAttributeBaseline,
NSLayoutAttributeNotAnAttribute = 0,
......//省略剩余
每一个枚举值代表了一个布局属性,名字都很直观就不一一解释了。注意,firstItem与secondItem不一定非得是同样的值,允许定义诸如某视图的高度等于另一个视图的宽度这样的约束。NSLayoutAttributeNotAnAttribute这个额外解释一下,当我们需要为某个视图精确指定一个宽度或者高度值时,这时候secondItem为nil,secondAttribute为NSLayoutAttributeNotAnAttribute。
relation定义了布局关系(NSLayoutRelation):
typedef NS_ENUM(NSInteger, NSLayoutRelation)
NSLayoutRelationLessThanOrEqual = -1,
NSLayoutRelationEqual = 0,
NSLayoutRelationGreaterThanOrEqual = 1,
布局关系可以是相等、大于等于或者小于等于。
multiplier即比例系数。constant即常量。
因此,每个约束就对应如下关系:
firstItem.firstAttribute {==,&=,&=} secondItem.secondAttribute * multiplier + constant   (公式3.2)
我们可以调用NSLayoutConstraint类的constraintWithItem:&方法,传入所有需要的参数构造一个新的约束。
理论就到此为止,下面我们还是以第二篇的例子来讲解如何使用代码添加约束。
打开Xcode,新建项目,选择iOS -& Application -& Single View Application。项目命名为AutoLayoutByConstraint,语言任意选择,设备选择Universal。下载苹果Logo图片apple.jpg,并将其拖入项目中。文件下载地址:
https://yunpan.cn/cfmJB82dfSwf6(提取码:4049)
首先在界面上方用来显示苹果Logo图片的是一个UIImageView,ViewController类的viewDidLoad方法如下:
- (void)viewDidLoad
[super viewDidLoad];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&apple.jpg&]];
logoImageView.contentMode = UIViewContentModeScaleAspectF
[self.view addSubview:logoImageView];
我们需要为logoImageView其添加4个约束:
- logoImageView左侧与父视图左侧对齐
- logoImageView右侧与父视图右侧对齐
- logoImageView顶部与父视图顶部对齐
- logoImageView高度为父视图高度一半
根据公式3.2,在ViewController类的viewDidLoad方法中构造上述4个约束,代码如下:
//logoImageView左侧与父视图左侧对齐
NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//logoImageView右侧与父视图右侧对齐
NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//logoImageView顶部与父视图顶部对齐
NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
//logoImageView高度为父视图高度一半
NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:0.0f];
//iOS 6.0或者7.0调用addConstraints
//[self.view addConstraints:@[leftConstraint, rightConstraint, topConstraint, heightConstraint]];
//iOS 8.0以后设置active属性值
leftConstraint.active = YES;
rightConstraint.active = YES;
topConstraint.active = YES;
heightConstraint.active = YES;
注意,我们需要将创建的约束添加到约束所涉及到的两个视图的最小公共祖先视图上。例如,如果直接设置视图的宽度和高度,则将约束添加到该视图即可;如果约束建立在父视图和子视图上,则添加到父视图上;如果约束建立在两个兄弟视图上,则添加到两个兄弟视图的父视图上。
对于上面的4个约束而言,涉及到的两个视图分别是logoImageView与其父视图self.view,这两个视图的最小公共祖先视图为self.view。UIView类提供了若干方法和属性,用于添加或者移除约束。对于iOS 6或者iOS 7可以调用addConstraint(s):和removeConstraint(s):方法;对于iOS 8及更新的版本,直接设置约束的active属性或者调用activateConstraints:与deactivateConstraints:类方法。
就是这么简单!现在编译并运行项目,
貌似logoImageView的尺寸不太对。如果在viewDidLoad方法中设置self.view的背景色为红色,看得会更清楚:
为什么会这样?这是由于苹果在iOS 6当中引入了自动布局的新概念,但在那时仍然有很多旧的代码使用autZ喎"/kf/ware/vc/" target="_blank" class="keylink">vcmVzaXppbmdNYXNr0+tzZXRGcmFtZTq1xLe9yr25ub2ovefD5qGjytTP66OsyOe5+72r0ru49tLRvq3J6NbDusNmcmFtZbKiyrnTw2F1dG9yZXNpemluZ01hc2u1xMrTzbzM7bzTtb3Su7j2yrnTw9fUtq+yvL7WtcTK08281tDKsaOs1MvQ0Mqx0OjSqtL+yr212L2rx7DV37XEZnJhbWW6zWF1dG9yZXNpemluZ01hc2vXqruvzqrX1Lavsry+1tS8yvijqNXi0KnS/sq916q7u7XE1LzK+LXEwODQzc6qTlNBdXRvcmVzaXppbmdNYXNrTGF5b3V0Q29uc3RyYWludKOpo6zV4tH5ssXE3MP3yLfG5M671sPT67PftOe2+LK7u+G1vNbC1LzK+LXEyLHKp6Gj1eK49tL+yr3Xqru7tcS5/bPMo6zKx9PJVUlWaWV3tcR0cmFuc2xhdGVzQXV0b3Jlc2l6aW5nTWFza0ludG9Db25zdHJhaW50c8r00NS1xNa1vva2qLXEoaPErMjPx+m/9s/Co6y4w9a1zqpZRVOjrLHtyr7Q6NKq1MvQ0Mqx19S2r7340NDS/sq916q7u6Gj1eK21NPavObI3b7JtcS0+sLrtbHIu8rHusO1xKOsyLu2+LWxztLDx8P3yLfOqsrTzbzM7bzTwcvUvMr4uvOjrM7Sw8e+zbK7z6PN+9TZvfjQ0GF1dG9yZXNpemluZ01hc2u1xNL+yr3Xqru7wcujrLfx1PK+zbvh0v3G8NS8yvi1xLPlzbuho9LytMujrNDo0qrM2LHw16LS4rXEysejrDxzdHJvbmc+tbHO0sPHyrnTw7T6wuu0tL2oytPNvMqxo6zQ6NKqvat0cmFuc2xhdGVzQXV0b3Jlc2l6aW5nTWFza0ludG9Db25zdHJhaW50c8r00NS1xNa1yejWw86qTk88L3N0cm9uZz6ho9Tadmlld0RpZExvYWS3vbeo1tC0tL2obG9nb0ltYWdlVmlld7XEtPrC69auuvOjrMztvNPI58/CtPrC66O6PC9wPg0KPHByZSBjbGFzcz0="brush:">
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&apple.jpg&]];
logoImageView.translatesAutoresizingMaskIntoConstraints = NO;
再次运行,这次就没问题了。
到这里,我想你应该可以把剩余的视图和约束的代码添加上了,全部代码如下:
- (void)viewDidLoad
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
UIImageView* logoImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@&apple.jpg&]];
logoImageView.translatesAutoresizingMaskIntoConstraints = NO;
logoImageView.contentMode = UIViewContentModeScaleAspectF
[self.view addSubview:logoImageView];
//logoImageView左侧与父视图左侧对齐
NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//logoImageView右侧与父视图右侧对齐
NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//logoImageView顶部与父视图顶部对齐
NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
//logoImageView高度为父视图高度一半
NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:logoImageView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:0.5f constant:0.0f];
//iOS 6.0或者7.0调用addConstraints
[self.view addConstraints:@[leftConstraint, rightConstraint, topConstraint, heightConstraint]];
//iOS 8.0以后设置active属性值
leftConstraint.active = YES;
rightConstraint.active = YES;
topConstraint.active = YES;
heightConstraint.active = YES;
UIScrollView* scrollView = [UIScrollView new];
scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:scrollView];
//scrollView左侧与父视图左侧对齐
NSLayoutConstraint* scrollLeftConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//scrollView右侧与父视图右侧对齐
NSLayoutConstraint* scrollRightConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//scrollView底部与父视图底部对齐
NSLayoutConstraint* scrollBottomConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
//scrollView顶部与logoImageView底部对齐
NSLayoutConstraint* scrollTopConstraint = [NSLayoutConstraint constraintWithItem:scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
scrollLeftConstraint.active = YES;
scrollRightConstraint.active = YES;
scrollBottomConstraint.active = YES;
scrollTopConstraint.active = YES;
UILabel* nameLabel = [UILabel new];
nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
nameLabel.text = @&苹果公司&;
nameLabel.backgroundColor = [UIColor greenColor];
[scrollView addSubview:nameLabel];
UILabel* descriptionLabel = [UILabel new];
descriptionLabel.translatesAutoresizingMaskIntoConstraints = NO;
descriptionLabel.text = @&苹果公司(Apple Inc. )是美国的一家高科技公司。由史蒂夫&乔布斯、斯蒂夫&沃兹尼亚克和罗&韦恩(Ron Wayne)等三人于日创立,并命名为美国苹果电脑公司(Apple Computer Inc. ), 日更名为苹果公司,总部位于加利福尼亚州的库比蒂诺。\n苹果公司创立之初主要开发和销售的个人电脑,截至2014年致力于设计、开发和销售消费电子、计算机软件、在线服务和个人计算机。苹果的Apple II于1970年代助长了个人电脑革命,其后的Macintosh接力于1980年代持续发展。该公司硬件产品主要是Mac电脑系列、iPod媒体播放器、iPhone智能手机和iPad平板电脑;在线服务包括iCloud、iTunes Store和App Store;消费软件包括OS X和iOS操作、iTunes多媒体、Safari网络浏览器,还有iLife和iWork创意和生产力套件。苹果公司在高科技企业中以创新而闻名世界。\n苹果公司日公开招股上市,2012年创下6235亿美元的市值记录,截至2014年6月,苹果公司已经连续三年成为全球市值最大公司。苹果公司在2014年世界500强排行榜中排名第15名。日,在宏盟集团的&全球最佳品牌&报告中,苹果公司超过可口可乐成为世界最有价值品牌。2014年,苹果品牌超越谷歌(Google),成为世界最具价值品牌 。&;
descriptionLabel.numberOfLines = 0;
descriptionLabel.backgroundColor = [UIColor yellowColor];
[scrollView addSubview:descriptionLabel];
//nameLabel左侧与父视图左侧对齐
NSLayoutConstraint* nameLabelLeftConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//nameLabel右侧与父视图右侧对齐
NSLayoutConstraint* nameLabelRightConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//nameLabel底部与descriptionLabel顶部对齐
NSLayoutConstraint* nameLabelBottomConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:descriptionLabel attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
//nameLabel顶部与父视图顶部对齐
NSLayoutConstraint* nameLabelTopConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
//nameLabel高度为20
NSLayoutConstraint* nameLabelHeightConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:20.0f];
nameLabelLeftConstraint.active = YES;
nameLabelRightConstraint.active = YES;
nameLabelBottomConstraint.active = YES;
nameLabelTopConstraint.active = YES;
nameLabelHeightConstraint.active = YES;
//descriptionLabel左侧与父视图左侧对齐
NSLayoutConstraint* descriptionLabelLeftConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//descriptionLabel右侧与父视图右侧对齐
NSLayoutConstraint* descriptionLabelRightConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//descriptionLabel底部与父视图底部对齐
NSLayoutConstraint* descriptionLabelBottomConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:scrollView attribute:NSLayoutAttributeBottom multiplier:1.0f constant:0.0f];
descriptionLabelLeftConstraint.active = YES;
descriptionLabelRightConstraint.active = YES;
descriptionLabelBottomConstraint.active = YES;
//nameLabel宽度与logoImageView宽度相等
NSLayoutConstraint* nameLabelWidthConstraint = [NSLayoutConstraint constraintWithItem:nameLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
//nameLabel宽度与logoImageView宽度相等
NSLayoutConstraint* descriptionLabelWidthConstraint = [NSLayoutConstraint constraintWithItem:descriptionLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:logoImageView attribute:NSLayoutAttributeWidth multiplier:1.0f constant:0.0f];
nameLabelWidthConstraint.active = YES;
descriptionLabelWidthConstraint.active = YES;
程序最终项目文件链接:https://yunpan.cn/cV2cmtNMYz6UA (提取码:0f38)
自动布局约束是通过描述视图间的关系而非强加坐标值来进行定位的,它更能满足不同设备尺寸的界面布局,并且更容易让人理解。虽然上面的代码很冗长,但每一句所描述的事实都十分清楚。在此省略自动布局的好处10000字。。。
区区几个简单的视图,就要写这么长的代码。。。
估计你看得有点眼花缭乱了吧,其实我也是修改并检查了好几次,又调试了好几次才完全写对的。在下一篇文章中,我将介绍另一种更简洁的方式,即使用VFL来添加约束,敬请期待吧。

我要回帖

更多关于 tablayout设置下划线 的文章

 

随机推荐