如何在登录活动中自定义标题栏

时间:2015-12-03 09:54:59

标签: android

我正在尝试为我的应用创建一个登录页面。我在Android-studio中创建活动时从选项中选择了登录活动。这就是我得到的:

enter image description here

我可以自定义email_login.xml文件中的电子邮件/密码字段和登录按钮。但是,我还需要自定义标题栏 - 例如,在中心对齐标题,在标题栏中添加后退按钮以及一些颜色修改。

问题是我无法在任何地方找到修改它的选项。以下是activity_login.xml文件的样子:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:gravity="center_horizontal"
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".LoginActivity">

<!-- Login progress -->
<ProgressBar android:id="@+id/login_progress" style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp" 
    android:visibility="gone" />

<ScrollView android:id="@+id/login_form" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:id="@+id/email_login_form" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <AutoCompleteTextView 
                android:id="@+id/email" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:hint="@string/prompt_email"
                android:inputType="textEmailAddress" 
                android:maxLines="1"
                android:singleLine="true" />

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText android:id="@+id/password" 
                android:layout_width="match_parent"
                android:layout_height="wrap_content" 
                android:hint="@string/prompt_password"
                android:imeActionId="@+id/login"
                android:imeActionLabel="@string/action_sign_in_short"
                android:imeOptions="actionUnspecified" 
                android:inputType="textPassword"
                android:maxLines="1" 
                android:singleLine="true" />

        </android.support.design.widget.TextInputLayout>

        <Button android:id="@+id/email_sign_in_button" style="?android:textAppearanceSmall"
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp" 
            android:text="@string/action_sign_in"
            android:textStyle="bold" />

    </LinearLayout>
</ScrollView>

4 个答案:

答案 0 :(得分:1)

你应该使用&#39; NoActionBar&#39;风格在&#39; styles.xml&#39;。 在布局中创建工具栏并将子视图添加到工具栏,设置标题并根据需要进行修改。

答案 1 :(得分:0)

更改AndroidManifest.xml中的标题

答案 2 :(得分:0)

您可以通过替换操作栏添加自定义标题栏。 这是如何..

1.创建一个新的xml并命名为custom_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@drawable/black_pattern" >

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAllCaps="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#fff"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:src="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="8dp"
        android:background="@null"
        android:src="@android:drawable/ic_menu_rotate" />

</RelativeLayout>
  1. 然后在您的活动代码中获取操作栏并将其替换为此自定义布局

    ActionBar mActionBar = getActionBar(); mActionBar.setDisplayShowHomeEnabled(false); mActionBar.setDisplayShowTitleEnabled(false); LayoutInflater mInflater = LayoutInflater.from(this); View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); TextView mTitleTextView = (TextView)mCustomView.findViewById(R.id.title_text); mTitleTextView.setText("My Own Title"); mActionBar.setCustomView(mCustomView); mActionBar.setDisplayShowCustomEnabled(true);

  2. 有关详细信息,请参阅thisthisthis

答案 3 :(得分:-1)

您必须使用空的Android活动,然后使用线性布局,您可以自己创建或自定义自己的标题栏。

相关问题