Android 如何在tablelayout中设置列之间的分隔符?
我想创建一个带有列分隔符的表。我想用一个竖条图像分割我的列。为了实现这一点,我使用了Android 如何在tablelayout中设置列之间的分隔符?,android,divider,android-tablelayout,Android,Divider,Android Tablelayout,我想创建一个带有列分隔符的表。我想用一个竖条图像分割我的列。为了实现这一点,我使用了“android:divider=“@drawable/abc”,但它不起作用。下面是我的xml文件: <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" a
“android:divider=“@drawable/abc”
,但它不起作用。下面是我的xml文件:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:stretchColumns="*"
android:padding="5dip"
android:divider="@drawable/tracking_green"
>
<TableRow >
<TextView
android:id="@+id/retaileritem1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="xxxxxxx" />
<TextView
android:id="@+id/retaileritem2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="xxxxxxx" />
<ImageView
android:id="@+id/retailerimage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/tracking_green" />
</TableRow>
由于tablelayout不直接提供此功能,一种可行的方法可能是框架布局。将所需表格放在另一个背景表格上,该背景表格只有一行,高度/宽度为“匹配父项”。添加3列所需布局权重。将垂直分隔符imageview添加到第二列。 主表也应该在其行中使用相同的布局\u weihts+边距,以使其外观良好 水平分隔符可以通过在“待分隔”表行之间添加imageview来实现 尚未测试,只是一种方法 ornay添加android:showDividers=“middle”
在带有垂直分隔符的列表中,使用ImageView,如下所示:
<ImageView
android:id="@+id/divider"
android:contentDescription="@string/divider"
android:layout_width="1dp"
android:layout_height="65dp"
android:layout_marginTop="8sp"
android:background="#3B3B3B"
/>
我想要的是这样一张桌子 我在styles.xml中添加了这个
<style name="Divider">
<item name="android:layout_width">1dip</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@color/divider_color</item>
</style>
<style name="Divider_invisible">
<item name="android:layout_width">1dip</item>
<item name="android:layout_height">match_parent</item>
</style>
Then in my table layout
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#92C94A" >
<TextView
android:id="@+id/textView11"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider_invisible" />
</LinearLayout>
<TextView
android:id="@+id/textView12"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/main_wo_colon"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
<TextView
android:id="@+id/textView13"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/side_wo_colon"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
<TextView
android:id="@+id/textView14"
android:paddingBottom="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="10dp"
android:text="@string/total"
android:textColor="@color/white"
android:textSize="16sp" />
</TableRow>
<!-- display this button in 3rd column via layout_column(zero based) -->
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#6F9C33" >
<TextView
android:id="@+id/textView21"
android:padding="5dp"
android:text="@string/servings"
android:textColor="@color/white"
android:textSize="16sp" />
<LinearLayout
android:layout_width="1dp"
android:layout_height="match_parent" >
<View style="@style/Divider" />
</LinearLayout>
..........
.......
......
1dip
匹配父项
@颜色/分隔器颜色
1dip
匹配父项
然后在我的表格布局中
..........
.......
......
我就是这样做的
代码
快照
尝试设置第二个文本视图marginleft=1dp。如果您不想设置分隔符marginleft=0dp回答这个问题已经很晚了,但下面是正确的方法 对于行之间的分隔符:
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@color/colorPrimary"
android:showDividers="middle">
对于柱之间的分隔器:
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/colorPrimary"
android:showDividers="middle">
解释:
中的分隔符标记用于将分隔符放置在其直接子对象之间,即行
当
中的分隔符标记用于在其直接子项之间放置分隔符时,即列是否也有方法更改分隔符的颜色。?例如:如果使用--android:divider=“@color/orangeBackground”,则可以在colors.xml处定义值,如--#FF8000
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:paddingBottom="50dp"
android:text="Salary Details"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textStyle="bold" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:shrinkColumns="0"
android:stretchColumns="1" >
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical" >
<TextView
android:layout_gravity="center_vertical"
android:gravity="left"
android:text="Financial Year"
android:textColor="@android:color/white"
android:textSize="14sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spnSearchByCity"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_gravity="center_vertical"
android:entries="@array/year"
tools:listitem="@android:layout/simple_spinner_dropdown_item" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d30059"
android:orientation="vertical"
android:paddingTop="50dp" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Month"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Net Salary"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_marginTop="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Click"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Jan"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="11305"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDetailsOneId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:text="DETAILS"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="Feb"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:text="11405"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="1"
android:background="#d30059"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtDetailsTwoId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:text="DETAILS"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="50dp" >
<Button
android:id="@+id/btnMainMenuId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="MAIN MENU"
android:textColor="@android:color/white"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:divider="@color/colorPrimary"
android:showDividers="middle">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="@color/colorPrimary"
android:showDividers="middle">