试图找到一个解决方案来更新事件上多个UI元素的多个约束.我已经看到了一些停用,进行更改,然后重新激活约束的示例,这种方法对于我正在使用的24个锚点似乎不切实际.
我的一组变化:
ticketContainer.translatesAutoresizingMaskIntoConstraints = false
ticketContainer.topAnchor.constraintEqualToAnchor(self.topAnchor).active = true
ticketContainer.leftAnchor.constraintEqualToAnchor(self.rightAnchor, constant: 20).active = true
ticketContainer.widthAnchor.constraintEqualToConstant(200.0).active = true
ticketContainer.leftAnchor.constraintEqualToAnchor(self.leftAnchor, constant: 20).active = true
ticketContainer.widthAnchor.constraintEqualToConstant(100.0).active = true
Run Code Online (Sandbox Code Playgroud) 这里有点新手.我进入Android开发大约两个月,但我在其他环境中有多年的开发经验.
好的.我有一个FloatingActionButton没有出现在我预期或想要的地方.这里面CoordinatorLayout,有一个沿AppBarLayout/ Toolbar和承接ListView.
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ViewVehicleList">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="Vehicle List"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="@+id/Vehicle_ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_AddVehicle"
style="@style/FloatingAddButton"
android:src="@drawable/ic_green_add"
android:layout_gravity="bottom|end"
app:layout_anchor="@id/Vehicle_ListView"
android:onClick="addVehicle"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
使用此布局,屏幕如下所示:

我layout_gravity说"bottom|end".我把它改成了"bottom|right",但我仍然得到了相同的结果.我已经阅读了很多教程,并通过Stack Overflow进行了研究,并且没有运气.
我设法通过删除FAB元素中列出的锚来解决它app:layout_anchor="@id/Vehicle_ListView",这似乎与我读过的内容相反:使用FAB并正确定位它需要使用layout_anchor和layout_gravity.没有锚标记,它看起来像这样:

所以这是我的问题:为什么我的锚固定了我的定位FloatingActionButton?我究竟做错了什么?
android android-layout layout-gravity floating-action-button layout-anchor
我已经以UIView编程方式创建了layout anchors.现在我想UILabel在这个视图中添加一个.到目前为止,这是我的代码:
let centerView = UIView()
centerView.translatesAutoresizingMaskIntoConstraints = false
centerView.backgroundColor = UIColor.white
view.addSubview(centerView)
centerView.leftAnchor.constraint(equalTo: view.leftAnchor, constraint: 20).isActive = true
centerView.rightAnchor.constraint(equalTo: view.rightAnchor, constraint: -20).isActive = true
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.text = "Testing"
label.textColor = UIColor.black
centerView.addSubview(label)
label.leftAnchor.constraint(equalTo: centerView.leftAnchor).isActive = true
Run Code Online (Sandbox Code Playgroud)
我认为这个标签会在参考时显示,centerView但它更多地是参考了UIWindow.这是当前的视图层次结构:
UIWindow --> UIView (centerView) --> UILabel (label)
Run Code Online (Sandbox Code Playgroud)
我需要在里面添加多个标签centerView,根据我的理解,这个链会变长,而我想要几个标签centerView
UIWindow
|
UIView (centerView)
/ | \
Label 1 Label 2 Label 3
Run Code Online (Sandbox Code Playgroud)
我怎样才能实现这种层次结构?
我有一个具有特定长宽比的矩形和一个具有相反长宽比的矩形。我想将它们安排在自己选择的网格布局中(相反,不必是常规网格:我更喜欢可以随意构建RowLayouts和ColumnLayouts 的解决方案)。
我知道我可以使用Layout.fillHeight和缩放布局中的项目Layout.fillWidth。不幸的是,我找不到合适的方法为定义纵横比Rectangle。我知道QML Image可以(通过其fillMode属性)做到这一点,但我认为没有简单的方法可以很好地做到这一点。
任何帮助或正确方向的指点将不胜感激!
注意,我假设要使用QML Layouts,但是如果有一个仅包含锚点或普通Row/ Column设置的功能解决方案,我将全力以赴!
还请注意,我宁愿将两种类型的面积保持Rectangle相同,就像在实验中看起来那样,这并不是那么简单...
编辑
我的意思是尝试减去等面积约束。矩形会填满宽度,但在高度上会留出空间,因为它们受纵横比和填充宽度的限制。高度应该一样,但是我不能将两者结合起来。

我想在我的两个相对布局之间有一个浮动动作按钮.
为此,我将父布局作为协调器布局,并将锚和锚重力指定为fab按钮.
但它没有被设定在我想要的地方.
我希望它设置在相对布局6的右下角,以及相对布局6和右角的相对布局3之间.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/relativeLayoutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="30dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:background="@color/colorAccent">
<RelativeLayout
android:id="@+id/relativeLayout6"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_centerHorizontal="true">
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/profile_img" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/relativeLayout6">
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchorGravity="center"
app:layout_anchor = "@id/linearLayout"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
请帮忙..谢谢.
例如,我有一个包含3个子视图的父视图.我以编程方式使用Layout Anchor并尝试实现以下布局
|--subview 1--|--subview 2--|--subview 3--|
Run Code Online (Sandbox Code Playgroud)
三个子视图中的每一个具有相等的宽度.换一种说法,
subView1.width = subView2.width = subView3.width subView1.width + subView2.width + subView3.width = parentView.width
我知道我可以使用乘法器来设置subView 1和subView 2的宽度:
subView1.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
subView2.widthAnchor.constraintEqualToAnchor(contentView.widthAnchor, multiplier: 1/3).active = true
Run Code Online (Sandbox Code Playgroud)
和subView3可以使前导锚与subView2的尾随锚对齐.
但是,我在某处看到Interface Builder实际上可以将这三个宽度直接分配(不使用常量或乘数).是否可以通过编程方式对Layout Anchor进行相同的操作?如何?谢谢.
我想在 iOS 中创建一个类似 android 的菜单。我正在使用布局约束来设置约束。当我尝试在单击按钮时更新图像的左侧约束时遇到了这个问题(它应该动画到单击按钮的位置)。谁能帮我?它应该支持横向和纵向。
我不想使用第三方代码,也不想使用NSLayoutconstraint.
这是我的代码。
class MenuViewController: UIViewController {
var buttons: [UIButton] = []
var imageView : UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
addMenuItems()
}
func addMenuItems() {
for _ in 0...4 {
let button : UIButton = UIButton()
self.view.addSubview(button)
button.backgroundColor = UIColor.darkGray
button.addTarget(self, action: #selector(didSelectedButton(_:)), for: .touchUpInside)
self.buttons.append(button)
}
for (index, button) in buttons.enumerated() {
button.setTitle(" \(index)", for: .normal)
if index …Run Code Online (Sandbox Code Playgroud) swift ×4
ios ×3
android ×2
autolayout ×2
aspect-ratio ×1
constraints ×1
java ×1
layout ×1
nsautolayout ×1
qml ×1
qt ×1