Ich bin extrem neu in Android Studio, und als Lernübung, ich versuche, eine Anwendung zu erstellen, ohne irgendwelche xml-Dateien außer Manifest zu verwenden. Ich möchte ein Aktivitäts-, Layout-, Werkzeugleisten- und Schubladenmenü vollständig innerhalb von Java erstellen. Ich weiß, dass es nicht so ist, wie Dinge normalerweise gemacht werden, und ich verursache mir wahrscheinlich viel unnötigen Stress, aber wie gesagt, dies ist eine Lernübung. Alles scheint auszuführen, aber die Symbolleiste wird nicht angezeigt. GetSupportActionBar(). GetHeight() gibt Null zurück.Android Studio Toolbar mit keine Layouts
Jede Hilfe ist willkommen!
Danke, John
Hier ist meine Activity.java
package com.example.john.myblankapplication;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
/**
* Created by John on 7/3/2016.
*/
public class MyActivity extends AppCompatActivity {
private RelativeLayout myRelativeLayout;
private LinearLayout myLinearLayout;
private Toolbar myToolbar;
private TextView myTextView;
private ActionBar myActionBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the main layout programmatically
createRelativeLayout();
createLinearLayout();
// I have tried it with LinearLayout and RelativeLayout
setContentView(myLinearLayout);
// Create the toolbar layout programmatically
createToolbarLayout();
//myLinearLayout.addView(myToolbar); // that turned the whole screen red
setSupportActionBar(myToolbar);
myActionBar = getSupportActionBar();
myActionBar.setDisplayHomeAsUpEnabled(true); // new test - didn't help
// Create the text programmatically
myLinearLayout.addView(createTextView());
Snackbar.make(myTextView, "ActionBarHeight="+myActionBar.getHeight(),
Snackbar.LENGTH_LONG)
.setAction("Action", null).show(); // displays 0
}
private void createRelativeLayout() {
myRelativeLayout = new RelativeLayout(this);
// Specifies the layout properties
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
myRelativeLayout.setLayoutParams(relativeParams);
}
private void createLinearLayout() {
myLinearLayout = new LinearLayout(this);
// Specifies the layout properties
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
);
myLinearLayout.setLayoutParams(linearParams);
}
private Toolbar createToolbarLayout() {
myToolbar = new Toolbar(this);
Toolbar.LayoutParams toolBarParams = new Toolbar.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
R.attr.actionBarSize, Gravity.TOP
);
myToolbar.setTitle("My Toolbar");
myToolbar.setLayoutParams(toolBarParams);
myToolbar.setBackgroundColor(Color.RED);
myToolbar.setVisibility(View.VISIBLE);
return myToolbar;
}
private TextView createTextView() {
myTextView = new TextView(this);
// Set initial layout parameters
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
// Set alignment parameters
//textViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
textViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
myTextView.setText("Here is some text");
myTextView.setLayoutParams(textViewParams);
return myTextView;
}
}
Nun, für den Anfang, sollte der Typ von 'LayoutParams' ein' View' haben sollte, hängt von seiner übergeordneten 'View' ab. ZB sollte in Ihrer 'Toolbar '' LinearLayout.LayoutParams' gesetzt sein. Als nächstes ist 'R.at.actionBarSize' nicht der tatsächliche Höhenwert. Schaut euch [diesen Beitrag] an (http://stackoverflow.com/questions/12301510/how-to-get-the-actionbar-height). –
Funktioniert gut. Vielen Dank. –