有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java AndroidStudio:如何在片段中移动按钮

我如何在一个片段中移动Android Studio元素,比如按钮或文本视图?在一个活动中,我可以使用拖放按钮;下降,但这似乎不适用于碎片。 所以现在我的按钮在左拐角,但是我希望把它们放在中间。

<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context="com.example.test.fragmenttest.BlankFragment">


    <Button
        安卓:id="@+id/button"
        安卓:layout_width="150dp"
        安卓:layout_height="wrap_content"
        安卓:text="Button" />

</FrameLayout>

共 (1) 个答案

  1. # 1 楼答案

    如果您使用的是片段或活动,则没有区别,这都与您的布局类型有关。尝试使用ConstraintLayout而不是您的,或者在框架布局中设置android:gravity="center"

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:text="Button" />
    
    
    </android.support.constraint.ConstraintLayout>
    

    或者:

    <FrameLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" 
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="center"/>
    
    </FrameLayout>