【问题标题】:Android: set button height same as widthAndroid:设置按钮高度与宽度相同
【发布时间】:2016-05-02 20:44:04
【问题描述】:

这是我的布局的一部分。我需要按钮是完美的正方形。一排会有4个按钮。滚动视图中将有更多行。每行都适合任何屏幕尺寸的屏幕宽度。我已经尝试了很多解决方案,但没有我想要的。然后我还需要将文本动态作为按钮,以便它每次都填充按钮宽度。有什么解决办法吗?我正在尝试这个几周。

<TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp">

            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:text="@string/r1960"
                android:textColor="#ffffff"
                android:textSize="@dimen/loptatext"
                android:shadowColor="#000000"
                android:shadowRadius="7"
                android:layout_weight="1"
                android:background="@drawable/tlacitko_stav" />

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:text="@string/r1964"
                android:textColor="#ffffff"
                android:textSize="@dimen/loptatext"
                android:shadowColor="#000000"
                android:shadowRadius="7"
                android:layout_weight="1"
                android:background="@drawable/tlacitko_stav" />

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:text="@string/r1968"
                android:textColor="#ffffff"
                android:textSize="@dimen/loptatext"
                android:shadowColor="#000000"
                android:shadowRadius="7"
                android:layout_weight="1"
                android:background="@drawable/tlacitko_stav" />

            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:text="@string/r1972"
                android:textColor="#ffffff"
                android:textSize="@dimen/loptatext"
                android:shadowColor="#000000"
                android:shadowRadius="7"
                android:layout_weight="1"
                android:background="@drawable/tlacitko_stav" />
        </TableRow>

【问题讨论】:

  • 你的问题听起来不可能。如果它们是屏幕的宽度,它们怎么可能是完美的正方形?另外,如果多个按钮都是屏幕的宽度,它们怎么可能排成一行?这没有任何意义。
  • 我的意思是在一起。行将适合任何屏幕宽度。 4个按钮并排。我已经编辑了问题。

标签: java android android-layout button


【解决方案1】:

您可以扩展 Button 并将其强制为方形,如下所示:

public class SquareButton extends Button {
    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public SquareButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }
}

【讨论】:

    【解决方案2】:

    我有一个非常相似的问题,我需要根据 arrayList 的大小在屏幕上动态添加按钮。据我所知,您无法使用 xml 执行此操作,并且必须在活动(或片段)中对视图进行编程

    按钮字符串示例:

       ArrayList<String> trending = new ArrayList<String>() {
        {
            add("one"); add("two"); add("three"); add("four"); add("five"); add("six"); add("eleven");
            add("seven"); add("eight"); add("nine");add("ten");
        }
    };
    

    这是编程布局中的按钮(我的是切换按钮)

      //LAYOUT SETTINGS 5
        TableLayout rLayout5 = new TableLayout(getActivity());
        rLayout5.setOrientation(TableLayout.VERTICAL);
        LayoutParams param7 = new LayoutParams(
                LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
        param7.addRule(RelativeLayout.BELOW, rLayout4.getId());
        rLayout5.setBackgroundColor(Color.parseColor("#EEEBAA"));
    
        rLayout5.setLayoutParams(param7);
        // List<ToggleButton> togButtStore = new ArrayList<ToggleButton>();
    
        int i = 0 ;
        while (i < trending.size()){
            if (i % 3 == 0){
                tr = new TableRow(getActivity());
                rLayout5.addView(tr);
            }
            ToggleButton toggleBtn = new ToggleButton(getActivity());
            toggleBtn.setText(trending.get(i));
            toggleBtn.setId(i);
            toggleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        Context context = getActivity().getApplicationContext();
    
                        CharSequence text = "Hello toast!";
                        int duration = Toast.LENGTH_SHORT;
                        Toast toast = Toast.makeText(context, text, duration);
                        toast.show();
                    } else {
                        // The toggle is disabled
                    }
                }
            });
            tr.addView(toggleBtn);
            i++;
        }
    

    您需要将您编写的每个布局添加到视图中

      root.addView(rLayout5);
      getActivity().setContentView(root);
    

    然后当然不要忘记返回视图(像往常一样)。

    我想给你一个带有按钮设置的布局设置,这可能有助于开始使用 Java 而不是 xml 来编写布局。顺便补充一句,你在xml中进行的任何布局和文本设置也可以在Java中进行设置,而且更加灵活。

    【讨论】:

      【解决方案3】:

      以编程方式设置按钮的高度和宽度非常棒,希望对您有所帮助..

      在您的类文件中输入以下代码。

          DisplayMetrics displaymetrics = new DisplayMetrics();
          getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
      
          int screenHeight = displaymetrics.heightPixels;
      
          int btnHeight = (int) (screenHeight * 0.25);
          int btnWidth = (int) (screenHeight * 0.25);
      
          button1.getLayoutParams().height = btnHeight;
          button1.getLayoutParams().width = btnWidth;
      
          button2.getLayoutParams().height = btnHeight;
          button2.getLayoutParams().width = btnWidth;
      
          button3.getLayoutParams().height = btnHeight;
          button3.getLayoutParams().width = btnWidth;
      
          button4.getLayoutParams().height = btnHeight;
          button4.getLayoutParams().width = btnWidth;
      

      我们在这里使用了 Displaymetrics,它将根据设备的屏幕尺寸更改您的按钮大小,此处按钮的宽度和高度是屏幕高度的 25%,您可以根据自己的方便进行更改。

      【讨论】:

        【解决方案4】:

        你应该这样做:

        Button button = (Button) findViewById.(R.id.button);
        
        int buttonWidth = button.getLayoutParams().width;
        button.setLayoutParams(new LayoutParams(buttonWidth, buttonWidth));
        

        希望这会对你有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-24
          • 2019-12-12
          • 1970-01-01
          • 1970-01-01
          • 2018-07-04
          • 1970-01-01
          • 2011-04-25
          相关资源
          最近更新 更多