she*_*kes 36 android android-layout
在iOS中,我非常喜欢删除故事板并使用制图框架将所有内容放在代码中.这是从Cartography的github中偷来的:
constrain(view1, view2) { view1, view2 in
view1.width == (view1.superview!.width - 50) * 0.5
view2.width == view1.width - 50
view1.height == 40
view2.height == view1.height
view1.centerX == view1.superview!.centerX
view2.centerX == view1.centerX
view1.top >= view1.superview!.top + 20
view2.top == view1.bottom + 20
}
Run Code Online (Sandbox Code Playgroud)
Android有什么相同的功能吗?似乎新的Constraint Layout是朝着正确方向迈出的一步,但我想以编程方式进行.
Mar*_*ini 61
游戏稍晚,但您需要基本上将约束布局中的视图视为具有自己的LayoutParams的常规视图.
在ConstraintLayout案例中,文档位于:https://developer.android.com/reference/android/support/constraint/ConstraintLayout.LayoutParams.html
此类包含指定视图如何在ConstraintLayout中布局的不同属性.要在运行时构建约束,建议使用ConstraintSet.
因此,推荐的方法是使用ConstraintSet.
那里有一个很好的代码示例,但核心概念是您需要创建一个新集(通过复制/克隆/新等),设置其属性,然后将其应用于您的布局.
例如:假设你的布局包含一个ConstraintLayout(这里称为mConstraintLayout),里面包含一个视图(样本中的R.id.go_button),你可以这样做:
ConstraintSet set = new ConstraintSet();
// You may want (optional) to start with the existing constraint,
// so uncomment this.
// set.clone(mConstraintLayout);
// Resize to 100dp
set.constrainHeight(R.id.go_button, (int)(100 * density));
set.constrainWidth(R.id.go_button, (int)(100 * density));
// center horizontally in the container
set.centerHorizontally(R.id.go_button, R.id.rootLayout);
// pin to the bottom of the container
set.connect(R.id.go_button, BOTTOM, R.id.rootLayout, BOTTOM, 8);
// Apply the changes
set.applyTo(mConstraintLayout);
// this is my… (ConstraintLayout) findViewById(R.id.rootLayout);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
34339 次 |
| 最近记录: |