首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编程工具栏

编程工具栏
EN

Stack Overflow用户
提问于 2018-02-03 00:24:47
回答 2查看 48关注 0票数 0

在我的Android作业中,我们必须创建一个简单的应用程序,只使用Java,而不是设计视图。我想向工具栏添加图标和文本。我可以使用设计视图让它工作,但我不能使用Java让它工作。

如果我的想法是对的,请给我建议。例如,为了向工具栏添加一个按钮,我创建了一个

android.widget.Button button = new android.widget.Button()

然后将button添加到Toolbar

我的onCreate()函数如下:

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    relative_layout = new RelativeLayout(this);
    relative_layout.setBackgroundColor(Color.WHITE);
    Toolbar tool_bar = new Toolbar(this);
    tool_bar.setTitle("Hello");
    tool_bar.setBackgroundColor(Color.RED);  // actual one I read from R.color.xyz
    tool_bar.setTitleTextColor(Color.WHITE);
    tool_bar.setId(1);

    // tool_bar.setNavigationIcon(R.drawable.ic_launcher_foreground);

    tool_bar.setPopupTheme(R.style.Theme_AppCompat_Dialog);
    this.setVisible(true);
    this.setSupportActionBar(tool_bar);

    RelativeLayout.LayoutParams toolbar_layout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    toolbar_layout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    toolbar_layout.addRule(RelativeLayout.CENTER_HORIZONTAL);

    Button button = new Button(this);
    button.setBackgroundColor(Color.GREEN);
    button.setText("Button");
    button.setId(2);

    RelativeLayout.LayoutParams button_layout = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    button_layout.addRule(RelativeLayout.CENTER_HORIZONTAL);
    button_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    // button_layout.setMargins(400, 0, 0, 0);

    tool_bar.addView(button, button_layout);

    relative_layout.addView(tool_bar, toolbar_layout);
    this.setContentView(relative_layout);

}

1)我想确认一下这是不是正确的方法?

2)我不能像图中那样将按钮移动到屏幕的右侧,尽管我使用了

代码语言:javascript
运行
复制
RelativeLayout.ALIGN_PARENT_RIGHT

如何才能将对齐方式设置为右对齐?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-03 00:30:55

添加此代码后,您的工具栏将如下所示:

使用此代码创建工具栏activity.xml代码:

代码语言:javascript
运行
复制
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbarAdjustScan"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:background="@color/colorPrimary"
    android:elevation="6dp"
    android:minHeight="56dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/titleToolbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:layout_weight="1"
            android:maxLines="1"
            android:textColor="@color/white"
            android:textSize="18dp" />

          <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:padding="10dp"
            android:id="@+id/searchAdjustBtn"
            android:layout_marginLeft="15dp"
            android:ellipsize="end"
            android:maxLines="1"
            android:text="SEARCH   "
            android:textColor="@color/white"
            android:textSize="13dp" />
    </LinearLayout>

</android.support.v7.widget.Toolbar>

Activity.java代码:

代码语言:javascript
运行
复制
Toolbar toolbarTop;

onCreate()调用setToolbar() inside

代码语言:javascript
运行
复制
 private void setToolbar() {
    toolbarTop = (Toolbar) findViewById(R.id.toolbarAdjustScan);
    setSupportActionBar(toolbarTop);
    TextView search = (TextView)toolbarTop.findViewById(R.id.searchAdjustBtn);
    TextView titleToolbar = (TextView)toolbarTop.findViewById(R.id.titleToolbar);
    titleToolbar.setText("TakeInventory");
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    getSupportActionBar().setElevation(0);

    search.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getApplicationContext(), TakeInventoryResult.class);
           // Bundle args = new Bundle();
           //  args.putSerializable("ARRAYLIST",(Serializable)inList);
           // intent.putExtra("BUNDLE",args);
            startActivity(intent);
            finish();
        }
    });

}
票数 1
EN

Stack Overflow用户

发布于 2018-02-03 00:29:20

工具栏只是一个ViewGroup。因此,就像以编程方式向任何ViewGroup添加视图一样,对工具栏进行同样的处理。

代码语言:javascript
运行
复制
Button bt = new Button(this);
bt.setText("Button");
LinearLayout.LayoutParams params = new 
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.FILL_PARENT);
params.gravity = Gravity.RIGHT;
button.setLayoutParams(params);
toolbar.addView(bt);

乐于提供帮助:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48586872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档