按钮没有得到适当的宽度android

时间:2017-10-02 18:48:58

标签: android android-layout android-xml android-tablelayout

我的TableLayout行包含Button。我希望所有按钮具有相同的宽度,但是当它们被渲染时它们的宽度为match_parent,即使我已经给它们指定了特定的宽度。它们不是在Java中的任何地方处理的,因此问题应该只存在于XML中。

这是我的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mridulahuja.kudamm.activities.ComponentDetailsActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/light_green_background"
        android:scaleType="fitXY"
        android:alpha="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        android:background="@null"
        android:scrollbars="vertical"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.mridulahuja.kudamm.tools.ExpandableTextView
                    android:id="@+id/txtComponentInfo"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:text="component info"
                    android:textColor="#000000"
                    android:textSize="20sp" />
            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_height="10dp"
                    android:layout_width="match_parent"
                    />

            </TableRow>


            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <Button
                    android:id="@+id/btnDataAtAGlance"
                    android:layout_height="wrap_content"
                    android:layout_width="250dp"
                    android:gravity="center"
                    android:textSize="18sp"
                    android:textColor="#ffffff"
                    android:background="@drawable/green_button_style"
                    android:text="Data at a glance"
                    />

            </TableRow>


            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_height="10dp"
                    android:layout_width="match_parent"
                    />

            </TableRow>


            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <Button
                    android:id="@+id/btnDimensionsAtAGlance"
                    android:layout_height="wrap_content"
                    android:layout_width="250dp"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:padding="10sp"
                    android:textSize="18sp"
                    android:textColor="#ffffff"
                    android:background="@drawable/green_button_style"
                    android:text="Dimensions at a glance"
                    />

            </TableRow>

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <View
                    android:layout_height="10dp"
                    android:layout_width="match_parent"
                    />

            </TableRow>


            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center">

                <Button
                    android:id="@+id/btnDownloadPdfCatalogue"
                    android:layout_height="wrap_content"
                    android:layout_width="250dp"
                    android:gravity="center"
                    android:layout_gravity="center"
                    android:padding="10sp"
                    android:textSize="18sp"
                    android:textColor="#ffffff"
                    android:background="@drawable/green_button_style"
                    android:text="PDF Catalogue"
                    />

            </TableRow>

        </TableLayout>

    </ScrollView>

</RelativeLayout>

这是在Android Studio上呈现的视图:

enter image description here

这是在移动设备上呈现的视图:(请参阅第一个按钮=&gt;当我使其XML类似时,所有按钮都会发生这种情况)

enter image description here

为什么会这样?

2 个答案:

答案 0 :(得分:1)

dp因设备而异。作为解决方法,您可以执行的操作是,将所有按钮的width设置为match_parent,并在每个按钮上添加marginLeftmarginRight。 或者根据易卜拉欣在评论中的建议,使用多个dimens.xml文件来获取各种密度。

答案 1 :(得分:0)

从每个按钮中删除android:layout_gravity="center",您已经为TableRow指定了android:gravity="center"(这是做同样的事情)。

通过android:layout_gravity="center"你说按钮应该在其父级内部居中,并且android:gravity="center"你要说这个元素的孩子(这是按钮)应该在它的中心。 Android先生很困惑,因此它将您设置的宽度更改为wrap_content并将按钮置于中心位置。

注意:您的第一个按钮没有指定layout_gravity,这就是它的原因       按预期显示