[Android] How to create a split line for a view

Source: Internet
Author: User

 

How to create a split line of a view, such as

Let's see three ways to create split lines that look good, such as adding a split line between buttons.

This example adds a split line between the three buttons within the linearlayout.

These three examples may be easy to achieve and there is a better way to achieve this.

1 manually adding linearlayout to the split line

We can create a view, the view is a split line, as long as the simple to add the split line between the button can be.

The implementation of the split line is as follows:

<view android:layout_height= "fill_parent" android:layout_width= "1DP" android:background= "#90909090" Android: Layout_marginbottom= "5DP" android:layout_margintop= "5DP"/>

  

So the whole layout, as pictured, becomes:

<linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:adjustViewBounds = "true" android:orientation= "horizontal" > <button android:layout_width= "fill_parent" Android:la yout_height= "Wrap_content" style= "? Android:attr/buttonbarbuttonstyle" android:text= "Yes" Android:layo ut_weight= "1" android:id= "@+id/button1" android:textcolor= "#00b0e4"/> <view android:layout_height = "Fill_parent" android:layout_width= "1px" android:background= "#90909090" android:layout_marginbottom= "5DP" android:layout_margintop= "5DP" android:id= "@+id/separator1"/> <button android:layout        _width= "Fill_parent" android:layout_height= "wrap_content" style= "? Android:attr/buttonbarbuttonstyle" android:text= "No" android:layout_weight= "1" android:id= "@+id/button2" android:textcolor= "#00b0e4"/&G     T <view android:layout_height= "Fill_parent" android:layout_width= "1px" android:background= "#90909090" Android:layout_ Marginbottom= "5DP" android:layout_margintop= "5DP" android:id= "@+id/separator2"/> <button A Ndroid:layout_width= "Fill_parent" android:layout_height= "wrap_content" style= "? android:attr/buttonbarbuttons Tyle "android:text=" Neutral "android:layout_weight=" 1 "android:id=" @+id/button3 "ANDROID:TEXTC Olor= "#00b0e4"/> </LinearLayout>

  

2 in LinearLayout definition divider

You can set a view divider for linearlayout, which is obviously a good solution, especially if you don't know how many child buttons are in LinearLayout.

This must be used at API level 11 or higher in the API version.

Let's define the split line style first:

  

Set the style of this split line to LinearLayout:

<linearlayout android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:adjustViewBounds     = "true" android:divider= "@drawable/separator" android:showdividers= "middle" android:orientation= "horizontal" > <button android:layout_width= "fill_parent" android:layout_height= "wrap_content" style= "? Androi        D:attr/buttonbarbuttonstyle "android:text=" Yes "android:layout_weight=" 1 "android:id=" @+id/button1 " Android:textcolor= "#00b0e4"/> <button android:layout_width= "Fill_parent" Android:layout_hei ght= "Wrap_content" style= "Android:attr/buttonbarbuttonstyle" android:text= "No" android:layout_weight = "1" android:id= "@+id/button2" android:textcolor= "#00b0e4"/> <button android:layout_width= "Fill_parent" android:layout_height= "wrap_content" style= "? Android:attr/buttonbarbuttonstyle" Android : text= "Neutral" android:layout_weight= "1" android:id= "@+id/button3" android:textcolor= "#00b0e4"/> </LinearLayout> 

  

The most important of these is, of course:

android:divider= "@drawable/separator" android:showdividers= "Middle  "
3 Set Buttonbarstyle for container components (default is split line, easiest to implement method)

As Danialgoodwin mentioned in the comments, adding the Buttonbarstyle to the linearlayout would show default separators. This is also-API level one or higher only.

The important part here, is adding this line to the LinearLayout:

<linearlayout    android:orientation= "Horizontal"    android:layout_width= "Fill_parent"    android: layout_height= "Fill_parent"    style= "Android:buttonbarstyle"    android:dividerpadding= "15DP"    >     <button        android:layout_width= "wrap_content"        android:layout_height= "Wrap_content"        android: text= "New button"        android:id= "@+id/button1"        android:layout_gravity= "center_vertical"/>     <!- -More buttons/views-     </LinearLayout>

  

You can also adjust the paddings of the view separators with the "dividerpadding" setting.

The button uses the same whistleblowing, so the spacing between them is also the same.

Of course you can set the gradient color for the split line.

Original: Http://envyandroid.com/archives/1193/view-separators

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.