2017-06-05 8 views
0

Ich verwende ConstraintLayout, um die Seite anzuzeigen, und ich finde den Weg nicht, den Titel in der Mitte der Leiste zu machen. Ich habe die Aktionsleiste oder die Werkzeugleiste nicht benutzt. Bei der Lösung, die ich online gefunden habe, geht es darum, ein Label auf einem aktiven Balken hinzuzufügen, das ich nicht habe. HierSo zentrieren Sie den Seitentitel in ConstraintLayout

ist das Layout:

<?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> 

und die Aktivität ist ziemlich einfach:

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

jede mögliche Hilfe geschätzt wird !!

+0

Sie sagen, dass u nicht Aktionsleiste oder die Symbolleiste verwenden, dann, wie u, das hier nicht in Ihrem Layout sehen es einstellen? – Pavan

+0

Ich habe es nicht in meinem Layout festgelegt. Ich habe nur den Titelinhalt in der Aktivität festgelegt. –

+0

dann ist es Action-Bar Sie verwenden Action Bar Thema – Pavan

Antwort

0

Überschreiben Sie die Methode setTitle (CharSequence title) der Aktivität.

public class MainActivity erweitert 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); 
} 
+0

Es funktioniert für mich, danke @sneha_km! –

Verwandte Themen