Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/367.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
Java 将按钮和文本视图居中_Java_Android_Center - Fatal编程技术网

Java 将按钮和文本视图居中

Java 将按钮和文本视图居中,java,android,center,Java,Android,Center,有人能帮我解决我的问题吗?在我桌子的最后一行,我有一个按钮,我希望它居中。下面是代码片段 <TableRow> <LinearLayout android:id="@+id/linearLayout2" android:layout_gravity="center" android:layout_height="fill_parent" android:layout_width="fill_parent"&

有人能帮我解决我的问题吗?在我桌子的最后一行,我有一个按钮,我希望它居中。下面是代码片段

<TableRow>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_gravity="center"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent">

        <Button
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"/>
    </LinearLayout>
</TableRow>

  • 为什么编码不起作用
  • 如何在按钮旁边放置文本视图

  • 谢谢。

    此线性布局布局或其TableRow父级可能无用

    试一下:

    <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_horizontal"
            android:text="Button" />
    
    
    
    
    
    执行以下操作:

    <TableRow>
    <LinearLayout
        android:id="@+id/linearLayout2"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    
        <Button
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:text="Button"></Button>
    </LinearLayout>
    
    
    

    您应该使用android:layout\u span=“max\uu number\u of\u columns”使相对长度宽度与父项匹配。
    在RelativeLayout内部,您可以将按钮放在中间,并将文本视图放在按钮easy旁边。
    试试这个代码

    <TableLayout>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2" // 2 is the number of col in your table
             >
             <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="button" />
             <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="aaa"
                android:layout_toRightOf="@id/button"
             />
    
            </RelativeLayout>
        </TableRow>
    </TableLayout>
    
    
    
    希望这有帮助

    <TableLayout>
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_span="2" // 2 is the number of col in your table
             >
             <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:text="button" />
             <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="aaa"
                android:layout_toRightOf="@id/button"
             />
    
            </RelativeLayout>
        </TableRow>
    </TableLayout>