【问题标题】:Android Adding Buttons to Toolbar ProgrammaticallyAndroid以编程方式将按钮添加到工具栏
【发布时间】:2017-10-28 08:03:24
【问题描述】:

我正在尝试以编程方式创建工具栏并在不使用 XML 的情况下添加左右栏按钮。

但是按钮没有向右对齐。

Toolbar TopTulBarVar = new Toolbar(this);
TopTulBarVar.setId(View.generateViewId());
TopTulBarVar.setBackgroundColor(Color.parseColor("#DDDDDD"));
TopTulBarVar.setTitle("Ttl Txt");
TopTulBarVar.setSubtitle("Dtl Txt");

Button NamBarBtnVar = new Button(this);
NamBarBtnVar.setText("Select");
NamBarBtnVar.setBackgroundColor(Color.GREEN);
LinearLayout.LayoutParams NamBarBtnRulVar = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
NamBarBtnRulVar.gravity = Gravity.RIGHT;
TopTulBarVar.addView(NamBarBtnVar, NamBarBtnRulVar);

也试过了

RelativeLayout.LayoutParams RloRulVar = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RloRulVar.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

如下图所示

但需要喜欢

【问题讨论】:

  • 能否添加一些截图以便更好地理解
  • 添加截图...

标签: java android alignment toolbar


【解决方案1】:

在活动代码中

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar t = (Toolbar) findViewById(R.id.tool);
    setSupportActionBar(t);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    //left side button

    Button b = new Button(this);
    Toolbar.LayoutParams l1=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l1.gravity = Gravity.START;
    b.setLayoutParams(l1);
    b.setText("left");
    t.addView(b);

    //center Textview
    TextView text=new TextView(this);
    text.setText("Text:1");
    Toolbar.LayoutParams l2 = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l2.gravity = Gravity.CENTER;
    text.setLayoutParams(l2);
    t.addView(text);

    //Right side button

    Button b1=new Button(this);
    b1.setText("Right");
    Toolbar.LayoutParams l3=new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.WRAP_CONTENT);
    l3.gravity=Gravity.END;
    b1.setLayoutParams(l3);
    t.addView(b1);

}

工具栏 XML 代码

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:background="@color/colorAccent"
android:id="@+id/tool"
app:contentInsetLeft="0dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
android:layout_height="wrap_content">

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

输出

【讨论】:

  • 你可以修改,所以我们也用 Java 代码而不是 XML 构建工具栏。我不想要 XML 中的任何内容,因为我想一次又一次地重用这段代码。
  • 也考虑投票给我的问题
  • 看起来无法动态添加工具栏我试过但没有锻炼(如果我找到方法,我会在以后编辑我的答案)
  • 这也可以与 MaterialButton 类按钮一起使用。
【解决方案2】:

对于当今为此苦苦挣扎的任何人:请确保在以编程方式添加视图时使用androidx.appcompat.widget.Toolbar.LayoutParams,因为我只是输入Toolbar.LayoutParams 被Android Studio 意外解析为android.widget.Toolbar.LayoutParams。我花了一些时间试图弄清楚为什么重力没有改变:如果在将视图添加到工具栏之前设置了错误的 layoutParams,则不会发生异常。我的布局中有androidx.appcompat.widget.Toolbar

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:

对我来说,我在 Toolbar 中添加了一个开关按钮,它确实有效:

            <Toolbar
                    android:id="@+id/toolbar"
                    style="@style/Toolbar"
                    android:visibility="visible"
                    android:background="@color/white">

                <Switch
                        android:id="@+id/btn_accessible"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_margin="10dp"
                        android:layout_weight="1"
                        android:visibility="visible"
                        android:paddingLeft="25sp"
                        android:paddingRight="25sp"
                        android:layout_centerHorizontal="true"
                        android:layout_centerVertical="true"
                        android:text=""
                        android:textColor="@color/white"
                        android:textSize="12sp"
                        android:thumb="@drawable/custom_switch_thumb"
                        android:track="@drawable/custom_switch_track"

                />


            </Toolbar>

在活动中我只是设置onClick listener

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    相关资源
    最近更新 更多