【问题标题】:Put blackline border around ScrollView - Android在 ScrollView 周围放置黑线边框 - Android
【发布时间】:2013-02-04 09:39:51
【问题描述】:

我的问题是,是否有办法在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>

下面的截图是它产生的:

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

【问题讨论】:

    标签: java android border scrollview


    【解决方案1】:

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

    <?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>
    

    【讨论】:

    • 当然,如果你想要黑色边框,把marginTop和marginBottom替换为margin。
    • 我有一张整个活动的背景图片,我想保留并在ScrollView 中显示。请查看我编辑的开场白。
    【解决方案2】:

    您可以将其背景设置为带有笔划的shape drawable

    例子:

    <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 的黑色边框的滚动视图。

    【讨论】:

    • 请看我编辑的开篇文章。我尝试了您的解决方案,但没有得到我想要的结果。
    【解决方案3】:

    Is there an easy way to add a border to the top and bottom of an Android View?

    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 的角度来看,这是最简单的,但您必须制作一个合适的九个补丁图像。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-30
        • 1970-01-01
        • 1970-01-01
        • 2022-11-04
        相关资源
        最近更新 更多