布局中的安卓绘图分隔线



我一直试图在我的布局之间画一条线,但无法显示或工作。

我已经把它放在我的activity_main.xml但没有任何显示。我试过改变颜色,但仍然一无所获。我也尝试将其放置在样式中,但我认为这不是放置它的正确位置,而且在编译它时我通常会遇到错误。

android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"/>

我的主要 xml 是。我现在还没有对样式进行任何更改.xml。

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team A" />
<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="threePoints"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="twoPoints"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="FreeThrow"
android:text="Free throw" />

</LinearLayout>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="Team B" />
<TextView
android:id="@+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="4dp"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="threePointsB"
android:text="+3 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="twoPointsB"
android:text="+2 Points" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="FreeThrowB"
android:text="Free throw" />

</LinearLayout>

</LinearLayout>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Reset"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="Reset"
/>


</RelativeLayout>

您可以简单地添加视图

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>

在样式 xml 中,您可以为垂直分隔符添加此代码。

<style name="Divider">
<item name="android:layout_width">1dp</item>
<item name="android:layout_height">match_parent</item>
<item name="android:background">@android:color/darker_gray</item>
</style>

然后在您的主活动 xml 中,您可以添加

<View style="@style/Divider" />

在两个线性布局之间,因此最终代码将沿着

<Linear Layout... for Team A/>
<View style="@style/Divider" />
<Linear Layout... for Team B/>

希望对您有所帮助。

要添加将用作分隔符的视图,如果使用LinearLayout,则可以插入如下内容(水平分隔符的示例(:

<View
android:layout_width = "match_parent"
android:layout_height = "1dp"
android:background... />

有关更复杂的方法,请参阅如下所示的内容:https://cyrilmottier.com/2014/11/17/grid-spacing-on-android/

最新更新