我是Android Studio的新手,作为一个学习练习,我试图创建一个应用程序,而不使用除manifest之外的任何xml文件。 我想在Java中完全创建活动,布局,工具栏和抽屉菜单。我知道这不是通常的事情,我可能会给自己带来很多不必要的压力,但正如我所说,这是一个学习练习。 一切似乎都在执行,但工具栏不显示。此外,getSupportActionBar()。getHeight()返回零。
感谢任何帮助!
谢谢, 约翰
这是我的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;
}
}
答案 0 :(得分:0)
我建议将工具栏设为自定义视图,并在布局屏幕中设置尺寸。如果导入工具栏包,则可以将自定义视图声明为工具栏。然后,您可以使用内置的所有工具栏命令来添加标题和项目。
希望这有帮助!
答案 1 :(得分:0)
您还需要将工具栏添加到线性布局。
// Create the toolbar layout programmatically
myLinearLayout.addView(// Create the toolbar layout programmatically
createToolbarLayout(););