我已经尝试了多种解决方案,但似乎没有一个有效!我目前正在使用以下Drawable
作为分隔线(这是水平示例,但相同的方法也适用于垂直,将高度切换为宽度)。
LinearLayout linearLayout; // set with findViewById
linearLayout.setDividerDrawable(getResources().getDrawable(R.drawable.divider));
linearLayout.setShowDividers(SHOW_DIVIDER_MIDDLE);
分隔符看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="10dp"
android:height="0dp" />
</shape>
不过,我希望能够以编程方式设置分隔线大小,因为直到运行时我才知道大小应该是多少。我尝试过创建一个ShapeDrawable
像这样:
int desiredWidth = 10;
LinearLayout linearLayout; // set with findViewById
ShapeDrawable shapeDrawable = new ShapeDrawable(new RectShape());
shapeDrawable.getPaint().setColor(Color.TRANSPARENT);
shapeDrawable.setIntrinsicWidth(desiredWidth);
linearLayout.setDividerDrawable(shapeDrawable);
linearLayout.setShowDividers(SHOW_DIVIDER_MIDDLE);
但分隔线并没有出现,物品只是粘在一起。我很确定这不是像素密度问题,因为无论我输入什么数字,我都会得到相同的效果 - 就好像可绘制对象实际上并没有采用我设置的大小。
您的可绘制高度为 0。请尝试以下操作:
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<size
android:width="10dp"
android:height="10dp" />
<!--android:height depends on the height you want for the horizontal line--->
</shape>
or add:
drawable.setIntrinsicHeight(height);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)