自定义翻页符,android"/>
android 自定义翻页符,android
android - 如何将(垂直)分隔符添加到水平LinearLayout?
我正在尝试将分隔符添加到水平线性布局但是无处可去。 分频器没有显示。 我是Android的新手。
这是我的布局XML:
xmlns:tools=""
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/llTopBar"
android:orientation="horizontal"
android:divider="#00ff00"
android:dividerPadding="22dip"
android:showDividers="middle"
>
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="asdf" />
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="asdf"
/>
12个解决方案
195 votes
用它作水平分隔线
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/honeycombish_blue" />
这适用于垂直分隔线
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/honeycombish_blue" />
或者,如果可以使用LinearLayout分频器,则用于水平分频器
在LinearLayout中
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@drawable/divider"
android:orientation="vertical"
android:showDividers="middle" >
如果你想使用垂直分频器代替android:showDividers的形状使用android:width="1dp"
提示:不要忘记android:showDividers项目。
Kapil Vats answered 2019-09-16T20:59:07Z
65 votes
试试这个,在android:divider文件夹中创建一个分隔符:
vertical_divider_1.xml
并使用LinearLayout中的android:divider属性,如下所示:
android:layout_width="match_parent"
android:layout_height="48dp"
android:orientation="horizontal"
android:divider="@drawable/vertical_divider_1"
android:dividerPadding="12dip"
android:showDividers="middle"
android:background="#ffffff" >
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
注意:android:divider仅适用于Android 3.0(API级别11)或更高版本。
Shreesha S answered 2019-09-16T20:59:50Z
33 votes
将分隔符添加到布局很容易,我们不需要单独的视图。
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:divider="?android:listDivider"
android:dividerPadding="2.5dp"
android:orientation="horizontal"
android:showDividers="middle"
android:weightSum="2" >
上面的代码为LinearLayout制作垂直分频器
khaintt answered 2019-09-16T21:00:20Z
15 votes
更新:使用AppCompat预蜂窝
如果您使用的是AppCompat库v7,则可能需要使用LinearLayoutCompat视图。 使用此方法,您可以在Android 2.1,2.2和2.3上使用可绘制分隔符。
示例代码:
xmlns:app=""
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:showDividers="middle"
app:divider="@drawable/divider">
drawable / divider.xml :(顶部和底部有一些填充的分隔符)
android:insetBottom="2dp"
android:insetTop="2dp">
非常重要的说明:LinearLayoutCompat视图不会延长LinearLayout因此你不应该使用android:showDividers或android:divider属性但自定义的:app:showDividers和app:divider.在代码中你还应该使用LinearLayoutCompat.LayoutParams而不是LinearLayout.LayoutParams!
Rolf ツ answered 2019-09-16T21:01:08Z
8 votes
我今天遇到了同样的问题。 如前面的答案所示,问题源于在分隔符标签中使用颜色,而不是可绘制的。 但是,我更喜欢尽可能使用主题属性,而不是编写自己的drawable xml。 您可以使用android:attr / dividerHorizontal和android:attr / dividerVertical来获取预定义的drawable:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:showDividers="middle"
android:divider="?android:attr/dividerVertical"
android:orientation="horizontal">
API 11及更高版本中提供了这些属性。
另外,正如bocekm在他的回答中所提到的,dividerPadding属性不会在垂直分隔符的任何一侧添加额外的填充,正如人们可能认为的那样。 相反,它定义了顶部和底部填充,因此如果它太大,可能会截断分隔符。
Nicolai Buch-Andersen answered 2019-09-16T21:01:47Z
4 votes
令人沮丧的是,您必须启用从活动中的代码中显示分隔符。 例如:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the view to your layout
setContentView(R.layout.yourlayout);
// Find the LinearLayout within and enable the divider
((LinearLayout)v.findViewById(R.id.llTopBar)).
setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
}
dougc answered 2019-09-16T21:02:13Z
4 votes
您可以使用内置分隔符,这将适用于两个方向。
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:attr/listDivider"
android:orientation="horizontal"
android:showDividers="middle">
Amilcar Andrade answered 2019-09-16T21:02:39Z
1 votes
由于dividerPadding太大,您的分隔符可能无法显示。 你设置了22dip,这意味着分隔符被顶部的22dip和底部的22dip截断。 如果您的布局高度小于或等于44dip,则不会显示分隔符。
bocekm answered 2019-09-16T21:03:05Z
0 votes
您必须为textview或imageview等分隔符创建任何视图,然后为其设置背景,如果您有图像,则使用颜色作为背景。
希望这对你有所帮助。
itsrajesh4uguys answered 2019-09-16T21:03:38Z
0 votes
为了得到绘制,LinearLayout的分隔符必须有一些高度,而ColorDrawable(基本上是#00ff00以及任何其他硬编码颜色)都没有。 解决这个问题的简单(和正确)方法是将您的颜色包装成具有预定义高度的Drawable,例如shape drawable
Dmitry Zaytsev answered 2019-09-16T21:04:05Z
0 votes
您可以使用ActionBarSherlock库中使用的IcsLinearLayout,或使用ICS上可用的常规API(或复制其代码)。
编辑:您也可以使用支持库中使用的LinearLayoutICS。 我在这里发了一篇关于它的帖子。
您可以使用相同的技术创建此处所示的drawable,或创建9-patch drawable。
android developer answered 2019-09-16T21:04:45Z
0 votes
如果Kapil Vats的答案不起作用,请尝试以下方法:
绘制/ divider_horizontal_green_22.xml
布局/ your_layout.xml
LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/llTopBar"
android:orientation="horizontal"
android:divider="@drawable/divider_horizontal_green_22"
android:showDividers="middle"
>
我遇到了padding属性不起作用的问题,因此我必须直接在分隔符中设置分隔符的高度。
注意:
如果要在垂直LinearLayout中使用它,请创建一个新的,如下所示:绘制/ divider_vertical_green_22.xml
xmlns:android="">
Ionut Negru answered 2019-09-16T21:05:39Z
更多推荐
android 自定义翻页符,android
发布评论