如何以编程方式在约束布局中移动视图(按钮)?

德瑞奇科技

我正在尝试创建一个简单的时间表,(我是初学者)。我为7天24小时制的时间表创建了一个基本布局,我的想法是通过编程方式插入按钮,通过操纵按钮的位置和宽度在正确的时间和持续时间上覆盖时间表,以便在单击时显示任务保存在下面的文本视图中,如下所示:

时间表安排

我知道如何创建按钮,但是经过数小时的寻找工作方法以将其大小和位置更改为所需值的尝试却没有成功,我转向这里寻求帮助。

作为测试,我制作了一个新的空项目,在设计视图中具有一个预制的约束视图,并尝试在其中放置一个按钮并将其移动。这是代码:


import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ConstraintLayout constraintLayout01 = (ConstraintLayout)findViewById(R.id.constraintLayout01); //a constraint layout pre-made in design view

        Button btn = new Button(this);
        btn.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT));
        constraintLayout01.addView(btn);

        int x = 100;
        int y = 5;
        ConstraintSet set = new ConstraintSet();
        set.connect(btn.getId(), ConstraintSet.LEFT, constraintLayout01.getId(), ConstraintSet.LEFT, x);
        set.connect(btn.getId(), ConstraintSet.TOP, constraintLayout01.getId(), ConstraintSet.TOP, y);
    }
}

该按钮出现在约束视图中,但是无论我如何更改x和y都不会改变位置。我究竟做错了什么?另外如何更改尺寸?

Edit1:根据要求向xml添加了一个按钮:

<androidx.constraintlayout.widget.ConstraintLayout 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/RootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/constraintLayout01"
        android:layout_width="390dp"
        android:layout_height="645dp"
        android:layout_marginStart="28dp"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/button2"
            android:layout_width="38dp"
            android:layout_height="41dp"
            android:layout_marginStart="100dp"
            android:layout_marginTop="100dp"
            android:text=""
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

编辑2:

添加set.clone和set.applyTo之后的logcat:

2020-03-23 10:24:56.111 10888-10888/? E/.example.test0: Unknown bits set in runtime_flags: 0x8000
2020-03-23 10:24:56.201 10888-10888/com.example.test02 I/Perf: Connecting to perf service.
2020-03-23 10:24:56.210 10888-10918/com.example.test02 E/Perf: Fail to get file list com.example.test02
2020-03-23 10:24:56.210 10888-10918/com.example.test02 E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2020-03-23 10:24:56.307 10888-10888/com.example.test02 W/.example.test0: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-03-23 10:24:56.308 10888-10888/com.example.test02 W/.example.test0: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-03-23 10:24:56.346 10888-10888/com.example.test02 D/AndroidRuntime: Shutting down VM
2020-03-23 10:24:56.347 10888-10888/com.example.test02 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test02, PID: 10888
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test02/com.example.test02.MainActivity}: java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
     Caused by: java.lang.RuntimeException: All children of ConstraintLayout must have ids to use ConstraintSet
        at androidx.constraintlayout.widget.ConstraintSet.clone(ConstraintSet.java:713)
        at com.example.test02.MainActivity.onCreate(MainActivity.java:29)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7397) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) 
乌斯曼胜利

这些行不见了

    btn.setId(View.generateViewId());
    set.clone(constraintLayout);
    set.applyTo(constraintLayout);

这是带有输出的工作解决方案。

public class Main_One extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_one);
        ConstraintLayout constraintLayout = (ConstraintLayout)findViewById(R.id.connstraint_layout); //a constraint layout pre-made in design view
        Button btn = new Button(this);
        btn.setText("StackFlow");
        btn.setLayoutParams(new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT));
        btn.setId(View.generateViewId());
        constraintLayout.addView(btn);
        int x = 100;
        int y = 505;
        ConstraintSet set = new ConstraintSet();
        set.clone(constraintLayout);
        set.connect(btn.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.LEFT, x);
        set.connect(btn.getId(), ConstraintSet.TOP, constraintLayout.getId(), ConstraintSet.TOP, y);
        set.applyTo(constraintLayout);
}

    }

XML格式

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:orientation="vertical">

   <androidx.constraintlayout.widget.ConstraintLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:id="@+id/connstraint_layout">

   </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何以编程方式添加视图和约束到ConstraintLayout?

如何以编程方式隐藏视图?

如何以编程方式设计线性布局

如何以编程方式添加容器视图

如何以编程方式创建布局约束

如何以编程方式禁用约束?

WPF GridSplitter:如何以编程方式移动?

如何以编程方式访问ConstraintLayout中视图的当前约束?

如何以编程方式修改约束布局?

如何以编程方式在约束布局中添加左右边距

当视图大小不同时,如何以编程方式设置约束?

如何以编程方式快速约束不同情况下的多次视图?

如何以编程方式正确移动对象?

如何以编程方式创建具有约束的视图

以编程方式在线性布局中移动布局位置

如何以编程方式在Google地图中移动一点

如何以编程方式在Android布局中添加新按钮

如何以编程方式在表视图页脚中设置UIView的约束?

如何以编程方式添加自动布局约束以使按钮在其视图底部上方50点?

如何以编程方式更改视图布局的属性

如何以编程方式关闭NSView子视图的可可自动布局?

如何以编程方式将布局包含在DrawerLayout中以及如何平滑拖动导航视图?

如何以编程方式分配UIViews自动布局边缘以匹配超级视图?

如何以编程方式移动表格?

Android:当布局中已经包含按钮时,如何以编程方式覆盖整个布局?

如何使用自动布局约束以编程方式更改按钮的位置并参考其他按钮?

我应该如何以编程方式更新动态视图的约束?

如何以编程方式访问约束数据?

如何以编程方式将视图从 XML 添加到布局?