[英]Center content of column with colspan and table borders on Android GridLayout
I am trying to create a table like this one using GridLayout: 我正在尝试使用GridLayout创建一个像这样的表:
But so far I have only achieved this approach: 但是到目前为止,我只实现了这种方法:
Using this code: 使用此代码:
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:rowCount="3"
android:columnCount="4"
android:orientation="horizontal"
android:background="@drawable/table_border" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/negro"
android:layout_columnSpan="4">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Peso de Bala"
android:textColor="@color/white"
android:gravity="center_horizontal"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2.59"
android:textColor="@color/negro"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="40"
android:textColor="@color/negro"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gramos"
android:textColor="@color/negro"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/white"
android:layout_columnSpan="2"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Granos"
android:textColor="@color/negro"/>
</LinearLayout>
</GridLayout>
The title seems to be centered correctly but the content is not well centered, what do I need to modify on my layout or what do I need to add for achieving this?, and also I don't know if I'm doing my <shape>
correctly. 标题似乎正确居中,但是内容居中,请问我需要在布局上进行哪些修改,或者需要添加哪些内容才能实现此目的?而且我也不知道我是否在做
<shape>
正确。
My shape code ( drawable/table_border.xml
) is like this: 我的形状代码(
drawable/table_border.xml
)是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/white"></solid>
<stroke android:width="5dp" android:color="@color/negro"/>
</shape>
The reason you are having trouble centering your text is that your view hierarchy doesn't support what you want to do; 您无法使文本居中的原因是视图层次结构不支持您想做的事情。 the cells in your GridLayout are only as wide as their widest child.
GridLayout中的单元格只有其最大子级的宽度。 If you are developing for API 21+ only, you could look into using weights for your cells.
如果仅针对API 21+开发,则可以考虑对单元使用权重。
I recommend re-thinking this layout and implementing it with ConstraintLayout
instead of GridView
. 我建议重新考虑此布局,并使用
ConstraintLayout
而不是GridView
来实现它。 This will allow your layout to work on all API levels. 这将使您的布局可在所有API级别上使用。
You can also achieve the black border by simply having a black background color on your ConstraintLayout's parent and a white background color on your ConstraintLayout itself. 您还可以通过在ConstraintLayout的父对象上简单地具有黑色背景色,并在ConstraintLayout本身上具有白色背景色来实现黑色边框。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:gravity="center_horizontal"
android:textColor="#fff"
android:text="Velocidad"/>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="#fff">
<TextView
android:id="@+id/value1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="18sp"
android:text="383"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/label2"/>
<TextView
android:id="@+id/label1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:text="Salida M/SEG"
app:layout_constraintTop_toBottomOf="@id/value1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/label2"/>
<TextView
android:id="@+id/value2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:gravity="center"
android:textSize="18sp"
android:text="1253"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toRightOf="@id/value1"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/label2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="12sp"
android:text="Muzzle PIE/SEG"
app:layout_constraintTop_toBottomOf="@id/value2"
app:layout_constraintLeft_toRightOf="@id/label1"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/value3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:textSize="18sp"
android:text="310"
app:layout_constraintTop_toBottomOf="@id/label1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/label2"/>
<TextView
android:id="@+id/label3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:gravity="center"
android:textSize="12sp"
android:text="91 MTS M/SEG"
app:layout_constraintTop_toBottomOf="@id/value3"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/label4"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="@+id/value4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:gravity="center"
android:textSize="18sp"
android:text="1017"
app:layout_constraintTop_toBottomOf="@id/label2"
app:layout_constraintLeft_toRightOf="@id/value3"
app:layout_constraintRight_toRightOf="parent"/>
<TextView
android:id="@+id/label4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:gravity="center"
android:textSize="12sp"
android:text="100 YDS PIE/SEG"
app:layout_constraintTop_toBottomOf="@id/value4"
app:layout_constraintLeft_toRightOf="@id/label3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.