Android 按钮的宽度不正确
我有一个Android 按钮的宽度不正确,android,android-layout,android-xml,android-tablelayout,Android,Android Layout,Android Xml,Android Tablelayout,我有一个表格布局,其中的行包含按钮。我希望所有按钮都具有相同的宽度,但在渲染时,即使我给了它们一个特定的宽度,它们的宽度也像是match\u parent。它们在Java中的任何地方都无法处理,因此问题应该只存在于XML中 以下是我的XML: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x
表格布局
,其中的行包含按钮
。我希望所有按钮都具有相同的宽度,但在渲染时,即使我给了它们一个特定的宽度,它们的宽度也像是match\u 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上呈现的视图:
这是在mobile上呈现的视图:(参见第一个按钮=>当我使所有按钮的XML相似时,所有按钮都会发生这种情况)
为什么会发生这种情况?
dp
会因设备而异。作为一种解决方法,您可以将所有按钮的宽度设置为match\u parent
,并在每个按钮上添加marginLeft
和marginRight
。
或者按照Ibrahim在评论中的建议,使用多个dimens.xml
文件以获得不同的密度。从每个按钮中删除android:layout\u gravity=“center”
,您已经为TableRow指定了android:gravity=“center”
(正在做相同的事情)
通过android:layout\u gravity=“center”
您是说按钮应该位于其父元素的中心,通过android:gravity=“center”
您是说此元素的子元素(即按钮)应该位于其内部的中心。安卓先生很困惑,所以它将您设置的宽度更改为wrap\u content
,并将按钮居中
注意:您的第一个按钮没有指定布局和重力,这就是为什么它是
按预期显示从设备到另一个设备的dp
值发生变化,您必须为每个密度创建多个dimens.xml文件。是否需要使用表结构?您可能必须使用其他视图元素,如约束/相对线。这些小部件更容易操作。@Ibrahim这种情况正在发生,即使我给它的宽度为50dp
,这是非常少的,也可以使用dp