I'm interested in adding dividers to a LinearLayout's children dynamically. I see in the docs that LinearLayout contains a CONST "SHOW_DIVIDER_MIDDLE" along with get and set divider methods. Can someone show me how I implement it? Thanks!
"This does not work"
layout xml:
<LinearLayout android:id="@+id/bar"
android:orientation="horizontal"
android:layout_height="40dip" android:layout_width="fill_parent"
android:background="@drawable/ab_backg开发者_如何转开发round_gradient" android:gravity="right|center_vertical">
<!-- sort button -->
<Button android:id="@+id/sortBtn" android:background="@drawable/defaultt"
android:layout_width="30dip" android:layout_height="30dip" android:onClick="sortThis" />
<!-- add button -->
<Button android:id="@+id/addBtn" android:background="@drawable/defaultt"
android:layout_width="30dip" android:layout_height="30dip" android:onClick="addThis" />
</LinearLayout>
main:
...
private void setupViews() {
//bar
mBar = (LinearLayout) findViewById(R.id.bar);
mBar.setDividerDrawable(R.drawable.divider);
}
You need to convert the Resource id you get back from R.drawable.divider
into a Drawable object, ala:
import android.content.res.Resources;
...
public void onCreate(Bundle savedInstanceState) {
...
Resources res = this.getResources();
LinearLayout layout = new LinearLayout(this);
layout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE | LinearLayout.SHOW_DIVIDER_BEGINNING | LinearLayout.SHOW_DIVIDER_END);
layout.setDividerDrawable(res.getDrawable(R.drawable.divider));
...
}
...
This assumes you've got a file named 'divider.jpg' (or similar) in your resources directory.
精彩评论