Android 如何在ConstraintLayout中将页面标题居中
我正在使用ConstraintLayout显示页面,但我找不到使标题位于栏中心的方法。我没有使用动作栏或工具栏。我在网上找到的解决方案是在活动条上添加一个我没有的标签 布局如下:Android 如何在ConstraintLayout中将页面标题居中,android,android-activity,android-constraintlayout,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"
<?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>
任何帮助都将不胜感激 重写活动的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);
}
您是说您没有使用操作栏或工具栏,那么您如何设置此处的操作栏或工具栏?我没有在布局中设置它。我只在活动中设置标题内容。然后是操作栏您正在使用操作栏组件,谢谢。我只是注意到有一个操作栏,尽管它没有显示在我的代码中。
@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);
}