创建我自己的操作自己的自定义栏

时间:2015-10-12 12:36:03

标签: android android-actionbar

我想创建一个看起来像动作栏的UI,尽管它不像一个行为。我想在UI中获得帮助。我的意思是我不想明确提供任何文本大小,样式或其他属性。我附上图片以使其更清晰。

enter image description here

我已经创建了UI。以下是代码。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/action_bar_grey"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/custom_tabs_grey"
        android:orientation="horizontal" >

        <TextView
            style="@style/Text.Bold"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/custom_tabs_grey"
            android:gravity="center_horizontal"
            android:paddingBottom="@dimen/padding_extra_wide"
            android:paddingTop="@dimen/padding_extra_wide"
            android:text="@string/waiting_to_upload"
            android:textColor="@android:color/black" />

        <View
            android:layout_width="@dimen/status_button_border_width"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/medium_narrow"
            android:layout_marginBottom="@dimen/medium_narrow"
            android:background="@android:color/darker_gray" />

        <TextView
            style="@style/Text.Bold"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@color/custom_tabs_grey"
            android:gravity="center_horizontal"
            android:padding="@dimen/padding_extra_wide"
            android:text="@string/uploaded"
            android:textColor="@android:color/black" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="@dimen/medium_narrow"
        android:layout_marginBottom="@dimen/medium_narrow"
        android:orientation="horizontal" >

        <FrameLayout
            android:id="@+id/upload_waiting"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="@dimen/medium_narrow"
            android:layout_weight="1" >
        </FrameLayout>

        <FrameLayout
            android:id="@+id/upload_done"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="@dimen/medium_narrow"
            android:layout_weight="1" >
        </FrameLayout>
    </LinearLayout>

</LinearLayout>

这里我明确给出了text属性,为分隔符添加了一个视图等等。我的问题是我必须明确地做所有事情。不是android提供任何特定的东西。就像我为分频器做的那样。但是背景和文字仍然不像普通标签那样准确。

<LinearLayout
    android:divider="?android:attr/actionBarDivider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    android:showDividers="middle" >

        <TextView
            style="?android:attr/buttonBarStyle" 
           ....MORE CODE

是否有任何建议或代码段可以提供帮助?

1 个答案:

答案 0 :(得分:1)

使用Toolbar

<android.support.v7.widget.Toolbar
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <TextView
        android:id="@+id/my_toolbar_title"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:gravity="center" />

</android.support.v7.widget.Toolbar>

您可以像使用任何其他视图一样使用Toolbar。调整大小,添加文字等。

public class MainActivty extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView toolbarTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        toolbar = (Toolbar) findViewById(R.id.my_toolbar);
        toolbarTitle = (TextView) findViewById(R.id.my_toolbar_title);

        setSupportActionBar(toolbar);
        toolbarTitle.setText("Waiting To Upload");
    }
}
相关问题