如何在ConstraintLayout中居中页面标题

时间:2017-06-05 14:53:58

标签: android android-activity android-constraintlayout

我正在使用ConstraintLayout来显示页面,而我找不到在标题栏中心标题的方法。我没有使用操作栏或工具栏。我在网上找到的解决方案就是在活动栏上添加标签,这是我不具备的。

这是布局:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="right"
    tools:context="com.spydertrap.occexample.MainActivity"
    tools:layout_editor_absoluteY="81dp"
    tools:layout_editor_absoluteX="0dp">

    <android.support.constraint.Guideline
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/guideline2"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="206dp" />


    <android.support.v7.widget.CardView
        android:id="@+id/cardView2"
        android:layout_height="150dp"
        android:layout_width="0dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline2"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/cardView"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:layout_marginBottom="50dp"
            app:srcCompat="@drawable/resource" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="116dp"
            android:layout_marginLeft="10dp"
            android:textAppearance="@style/CardSubTitle"
            android:text="Resource Center" />
    </android.support.v7.widget.CardView>
</android.support.constraint.ConstraintLayout>

活动非常简单:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setTitle("Dashboard");
    }
}

This title needs to be centered

任何帮助将不胜感激!!

1 个答案:

答案 0 :(得分:0)

覆盖Activity的setTitle(CharSequence title)方法。

公共类MainActivity扩展了AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setTitle("Dashboard");

}

@Override
public void setTitle(CharSequence title) {
    super.setTitle(title);

    TextView textView = new TextView(this);
    textView.setText(title);
    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    textView.setGravity(Gravity.CENTER_HORIZONTAL);
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(textView);
}