Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中以编程方式添加按钮后的TextView位置移动_Android_Android Fragments - Fatal编程技术网

在Android中以编程方式添加按钮后的TextView位置移动

在Android中以编程方式添加按钮后的TextView位置移动,android,android-fragments,Android,Android Fragments,我有一个关于在Android中以编程方式向片段添加按钮的问题。下面是我的片段的xml布局 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app=&qu

我有一个关于在Android中以编程方式向片段添加按钮的问题。下面是我的片段的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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_blank_01"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment_01">

    <!-- TODO: Update blank fragment layout -->
    <ImageView
        android:layout_width="800px"
        android:layout_height="480px"
        android:src="@color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/myTextView_Text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is Text #1"
        android:textSize="35px"
        android:textColor="@color/white"
        android:layout_marginTop="200px"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/myTextView_Text_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Here is Text #2"
        android:textSize="25px"
        android:textColor="@color/white"
        android:layout_marginTop="260px"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
当通过模拟器进行模拟时,文本视图的位置会发生变化


请问我的代码出了什么问题?非常感谢

但是预期的结果是什么呢?我预期TextView的原始布局不会改变。我使用文本视图的约束布局,它们应该水平居中。
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_blank_01, container, false);

        //==========================================================================================//

        ConstraintLayout r1 = (ConstraintLayout) view.findViewById(R.id.fragment_blank_01);

        Button TestButton = new Button(getActivity());
        TestButton.setId(0);
        TestButton.setHeight(75);
        TestButton.setWidth(250);
        TestButton.setX(275);
        TestButton.setY(330);
        TestButton.setText("Button");
        TestButton.setBackgroundColor(0xffffffff);
        r1.addView(TestButton);

        //==========================================================================================//   
        
        return view;
    }