Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在工具栏布局中绘制线条/边框?_Android_Android Layout - Fatal编程技术网

Android:在工具栏布局中绘制线条/边框?

Android:在工具栏布局中绘制线条/边框?,android,android-layout,Android,Android Layout,我要求appBar的外观如下: <android.support.v7.widget.Toolbar> <RelativeLayout> <TextView> <ImageView> <View android:layout_width="match_parent" android:layout_height="1dp"

我要求appBar的外观如下:

<android.support.v7.widget.Toolbar>
    <RelativeLayout>
         <TextView>
         <ImageView>
         <View  android:layout_width="match_parent" 
                android:layout_height="1dp"
                android:layout_below="@+id/button_menu"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

背景是透明的

我使用此布局获取appBar(隐藏详细信息):


只需在带有
layout\u gravity=“bottom
ImageView
下方添加一个高度为1dp的视图将不起作用


甚至可以在
工具栏布局中画一条线或一个边框吗?

使用如下的RelativeLayout:

<android.support.v7.widget.Toolbar>
    <RelativeLayout>
         <TextView>
         <ImageView>
         <View  android:layout_width="match_parent" 
                android:layout_height="1dp"
                android:layout_below="@+id/button_menu"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>

将工具栏包装在AppBarLayout(扩展LinearLayout)中,并将线条添加为视图:

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    ...>

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        .../>

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

</android.support.design.widget.AppBarLayout>

还有另一种解决方案,通过绘制到画布:

class CustomToolbar(context: Context?, attrs: AttributeSet?) : Toolbar(context, attrs) {

    private val lineColor: Int = Color.rgb(0, 0, 0)

    init {
        invalidate()
    }

    override fun dispatchDraw(canvas: Canvas?) {
        val paint = Paint()
        paint.color = lineColor
        paint.strokeWidth = 15f
        canvas?.drawLine(0f, height.toFloat(), width.toFloat(), height.toFloat(), paint)
        super.dispatchDraw(canvas)
    }
}

您可以创建一个这样的图像,并将其设置为Backburnd to Toolbar,而不是下面的
android:layout\u
我建议
android:layout\u alignParentBottom=“true“
。这样你就不会依赖于其他任何视图,遗憾的是,这并没有把这条线画得很宽。
工具栏
左侧仍有一些空白或填充。如果您设置
HomeAsUpIndicator
(例如图像),此边距会更大,因为此图像周围有更多的边距/填充。@McOzD您可以尝试以下方法:app:contentInsettStart=“0dp”
app:contentInsettStart=“0dp”
没有为我这样做,您没有结束TextView和ImageView。这是一个编译时错误!你打算怎么做?????这应该是公认的答案,但稍作修改。所需线条由
AppBarLayout
添加,无需1dp高视图,除非您的目标低于API 21或需要自定义线条。
class CustomToolbar(context: Context?, attrs: AttributeSet?) : Toolbar(context, attrs) {

    private val lineColor: Int = Color.rgb(0, 0, 0)

    init {
        invalidate()
    }

    override fun dispatchDraw(canvas: Canvas?) {
        val paint = Paint()
        paint.color = lineColor
        paint.strokeWidth = 15f
        canvas?.drawLine(0f, height.toFloat(), width.toFloat(), height.toFloat(), paint)
        super.dispatchDraw(canvas)
    }
}