【问题标题】:Android + setDividerDrawable on a LinearLayout?线性布局上的Android + setDividerDrawable?
【发布时间】:2014-03-13 06:22:47
【问题描述】:

我有兴趣动态地将分隔线添加到 LinearLayout 的子项中。我在文档中看到 LinearLayout 包含一个 CONST“SHOW_DIVIDER_MIDDLE”以及获取和设置分隔符方法。有人可以告诉我如何实现它吗?谢谢!

“这不起作用”

布局 xml:

<LinearLayout android:id="@+id/bar"
        android:orientation="horizontal" 
        android:layout_height="40dip" android:layout_width="fill_parent"
        android:background="@drawable/ab_background_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>

主要:

...
private void setupViews() {
        //bar
        mBar = (LinearLayout) findViewById(R.id.bar);
        mBar.setDividerDrawable(R.drawable.divider);
}

【问题讨论】:

    标签: android drawable android-linearlayout


    【解决方案1】:

    您需要将从R.drawable.divider 获取的Resource id 转换为Drawable 对象,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));
    
        ...
     }
    ...
    

    这假设您的资源目录中有一个名为“divider.jpg”(或类似文件)的文件。

    【讨论】:

    • 这可行,但请注意 setShowDividers 是在 API 级别 11(Honeycomb)中添加的。 stackoverflow.com/questions/8304221/…
    • @worked 如果您愿意,您可以使用 ActionBarSherlock 中的 IcsLinearLayout,但请注意它不打算使用。支持库中还有 LinearLayoutICS
    • Xamarin 开发者使用这个 _tabLayout.SetDividerDrawable(Resources.GetDrawable(Resource.Drawable.divider));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-21
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多