7

我的问题是,是否有办法在 a 周围放置一个简单的黑线边框,ScrollView以便用户准确了解ScrollView起点、终点和宽度。我在 Android 文档中找不到任何类型的 XML 或 java 代码说明如何执行此操作。我知道这必须是可能的。

下面是我ScrollView需要有边框的 XML 代码。

<LinearLayout
          .... />


    <TextView
        ... />

//BORDER STARTS HERE        
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="94.5"
        android:paddingBottom="5dp"
        android:fillViewport="true" >

        <LinearLayout
            ... > 

            <TextView
                ... />

        </LinearLayout>
    </ScrollView>
//BORDER ENDS HERE

    <Button
        ... />
</LinearLayout>

编辑

我刚刚添加了一个scrollviewborder.xml带有以下代码的背景作为ScrollView.

<?xml version="1.0" encoding="utf-8"?>
<shape 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="#000000"/>
</shape>

下面的截图是它产生的:

在此处输入图像描述

这不是我想要的。我想要一条细黑线,而不是黑框。

4

4 回答 4

11

您可以将其背景设置为可通过描边绘制的形状。

例子:

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0dip"
    android:layout_weight="94.5"
    android:paddingBottom="5dp"
    android:background="@drawable/stroke_bg"
    android:fillViewport="true" >

在您的 drawables 文件夹中,有一个 stroke_bg.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="2dp"
        android:color="#000000"/>
</shape>

这应该会产生一个带有宽度为 2dp 的黑色边框的滚动视图。

于 2013-02-19T16:17:04.233 回答
11

根据您的新要求,请尝试以下代码:

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    <solid
        android:color="@android:color/transparent"/>
    <stroke
        android:width="2dp"
        android:color="#000000"/>
</shape>
于 2013-02-19T16:17:27.767 回答
2

有没有一种简单的方法可以在 Android 视图的顶部和底部添加边框?

我能想到的Android中没有内置的“边框”概念。您可以通过布局来模拟一个,例如:

将 TextView 包装在 LinearLayout 中,并在其上方和下方添加一个普通 View,这些 View 具有所需的 android:background 颜色和适当的 android:layout_height(例如,1px、1dip)。

将 TextView 包装在 LinearLayout 中,并在上方和下方添加带有所需边框图像的 ImageView 小部件。

将 TextView 包裹在 RelativeLayout 中,添加一个固定在顶部的普通视图(具有适当的背景和高度),另一个固定在底部的普通视图,并将您的 TextView 固定在顶部和底部。这利用了 RelativeLayout 的 z 轴支持,因此边框将位于 TextView 占用的空间内,而不是像前两种方法那样位于 TextView 之外。

为 TextView 提供一个包含九个补丁的 PNG 文件作为具有边框的背景。从 XML 的角度来看,这是最简单的,但是您必须制作一个适当的九个补丁图像。

于 2013-02-19T16:17:44.180 回答
2

不要像某些人建议的那样将其包装在布局中。这将使您的应用程序的渲染速度变慢一些(可能不明显,但仍然如此)。相反,创建一个只定义一个孩子的形状并将其用作 ScrollView 的背景。

于 2013-02-19T16:24:25.580 回答