【问题标题】:Android Studio: Button alignment in table rowAndroid Studio:表格行中的按钮对齐
【发布时间】:2017-09-14 12:34:01
【问题描述】:

我在 android Studio 中开发和应用程序,现在我遇到了一个奇怪的问题。

我有一个tableRow,其中包含一个TextView 和一个Button

我正在以编程方式构建它们,我希望tableRow内的button与屏幕右侧对齐。

我已经尝试了很多选项,例如:

button.setGravity(Gravity.RIGHT)button.setGravity(Gravity.END) 并尝试在layout_width 中使用fill_parent 为表格提供布局参数,但似乎不起作用。

这是我构建 TableRow 的代码部分:

  TableRow new_tablerow = new TableRow(getContext());
                        layout_pickup_passengers.addView(new_tablerow);

                        LinearLayout.LayoutParams rowParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        new_tablerow.setLayoutParams(rowParams);

                        //GLOBAL PASSENGERS
                        TextView text_pass_type = new TextView(getContext());
                        new_tablerow.addView(text_pass_type);
                        text_pass_type.setTextColor(getResources().getColor(R.color.black));
                        text_pass_type.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
                        text_pass_type.setText("> Passageiros");

                        //BUTTON
                        // Button
                        final Button button_numberPickerDialog = new Button(getContext());
                        button_numberPickerDialog.setBackgroundResource(R.color.wallet_bright_foreground_holo_dark);
                        new_tablerow.addView(button_numberPickerDialog);
                        button_numberPickerDialog.setGravity(Gravity.RIGHT);

这就是我得到的:

所以您是否只能看到里面的文本向右对齐(0 数字),我希望按钮向右对齐而不是里面的文本。

【问题讨论】:

    标签: java android-studio button


    【解决方案1】:

    您这里提到的方法“setGravity(int)”不能像您想象的那样工作,因为该方法的工作与xml中的属性“andriod:gravity”相同,显然不会影响您定义的按钮。

    实际上,我想知道您为什么坚持以编程方式构建它们。我想如果你用 xml 构建它们,使用“android : layout_gravity”可以工作。


    我尝试使用RaletiveLayout 来完成这项工作,代码如下:

    //TableRow new_tablerow = new TableRow(getBaseContext());
            //layout_pickup_passengers.addView(new_tablerow);
    
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    
            RelativeLayout relativeLayout = new RelativeLayout(this);
            layout_pickup_passengers.addView(relativeLayout,params);
    
            //GLOBAL PASSENGERS
            TextView text_pass_type = new TextView(getBaseContext());
            RelativeLayout.LayoutParams lp_text = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            lp_text.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            text_pass_type.setTextColor(getResources().getColor(R.color.colorPrimary));
            text_pass_type.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
            text_pass_type.setText("> Passageiros");
            relativeLayout.addView(text_pass_type,lp_text);
    
            //BUTTON
            // Button
            final Button button_numberPickerDialog = new Button(getBaseContext());
            button_numberPickerDialog.setBackgroundResource(R.color.colorPrimaryDark);
            RelativeLayout.LayoutParams lp_button = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
            );
            lp_button.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            relativeLayout.addView(button_numberPickerDialog,lp_button);
    

    也许我对您的代码进行了一些更改。 它可以牺牲你吗? enter image description here

    【讨论】:

    • 我不能在 xml 中构建它们,因为它是一个预订应用程序,这部分取决于应用程序中以前的用户选择
    • 好吧,对我来说,也许我会尝试使用RelativeLayout作为视图来代替
    • 我需要一个TableLayout,因为在某些情况下我需要向它添加更多tableRows,并且它需要对齐
    • 对不起,我没能找到另一个有效的解决方案。G'Luck!
    猜你喜欢
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    • 2012-02-11
    • 2013-04-18
    • 2019-02-03
    • 2014-02-27
    • 2012-12-01
    • 1970-01-01
    相关资源
    最近更新 更多