【问题标题】:Why are adjacent Buttons moving when I change the size of the text in a Button当我更改按钮中文本的大小时,为什么相邻的按钮会移动
【发布时间】:2016-10-11 20:00:52
【问题描述】:

这是每个Button 具有相同大小文本时的布局:

这是我增加右下角Button的文字大小时的布局:

这里发生了什么?为什么Button的“0”移动低于相邻的两个Buttons

这是布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="center"
              android:background="@android:color/white"
              android:orientation="vertical"
              android:padding="6dp">

    <TextView
            android:id="@+id/input_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textColorHint="@color/hint_color"
            android:singleLine="true"
            android:textSize="40sp"/>

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="3">

        <Button
            android:id="@+id/the_1_button"
            android:background="@android:color/white"
            android:text="1"/>

        <Button
            android:id="@+id/the_2_button"
            android:background="@android:color/white"
            android:text="2"/>

        <Button
            android:id="@+id/the_3_button"
            android:background="@android:color/white"
            android:text="3"/>

        <Button
            android:id="@+id/the_4_button"
            android:background="@android:color/white"
            android:text="4"/>

        <Button
            android:id="@+id/the_5_button"
            android:background="@android:color/white"
            android:text="5"/>

        <Button
            android:id="@+id/the_6_button"
            android:background="@android:color/white"
            android:text="6"/>

        <Button
            android:id="@+id/the_7_button"
            android:background="@android:color/white"
            android:text="7"/>

        <Button
            android:id="@+id/the_8_button"
            android:background="@android:color/white"
            android:text="8"/>

        <Button
            android:id="@+id/the_9_button"
            android:background="@android:color/white"
            android:text="9"/>

        <ImageButton
            android:id="@+id/the_backspace_button"
            style="@style/Widget.AppCompat.Button"
            android:background="@android:color/white"
            android:src="@drawable/ic_backspace"/>

        <Button
            android:id="@+id/the_0_button"
            android:background="@android:color/white"
            android:text="0"/>

        <Button
                android:id="@+id/the_done_button"
                android:layout_width="88dp"
                android:layout_height="48dp"
                android:minWidth="0dp"
                android:minHeight="0dp"
                android:background="@android:color/white"
                android:text="done"/>
    </GridLayout>

</LinearLayout>

更新:

我将“DONE”Button 的文本设置为超大——100sp——并将maxWidthmaxHeight 设置为等于另一个Buttons 的高度和宽度。结果如下:


*蓝色是GridLayout的背景,红色是“0”Button's,黄色是“DONE”Button's

如果Button 的大小从不改变,为什么更改“完成”按钮的文本大小会影响特定Button 以外的任何内容?

【问题讨论】:

  • 我无法回答您的具体问题,但在android:id="@+id/the_0_button" 上设置android:layout_gravity="top" 应该会移动Button。您应该仔细阅读源代码,看看GridLayout 是如何测量和布置其子级的。
  • @Vikram 试过了。没用。

标签: android android-layout


【解决方案1】:

我认为完成按钮有问题。你设置它的背景。然后您还将其文本设置为“完成”。

只需将其文本设置为空,问题就可以解决;)

【讨论】:

  • 按钮必须有文字
  • "-" 不是图像,而是文字连字符。当文本大小改变时,文本也变为“-”
  • 如果您查看第一张图片,您会看到“完成”作为按钮文本
【解决方案2】:

如果使用 grid:layout_rowWeight 或 grid:layout_rowHeight 属性对每个按钮都一样呢?

看看这个: https://stackoverflow.com/a/32291319/2205825

【讨论】:

    【解决方案3】:

    试试这个.....

    使用GridView 而不是许多Button 不会给您带来任何布局问题(视图调整)

    查看下面的示例,它可以解决您关于按钮调整的问题

    使用这个layout 而不是你的布局.....

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="6dp">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
        <TextView
            android:id="@+id/input_textview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:singleLine="true"
            android:textSize="40sp" />
    
        </LinearLayout>
    
        <GridView
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:columnWidth="100dp"
            android:gravity="center"
            android:numColumns="3" />
    
    </LinearLayout>
    

    在您的 OnCreate 方法中添加这两行.....

      GridView gridview = (GridView) findViewById(R.id.gridview);
        gridview.setAdapter(new CustomAdapter(this));
    

    在你的项目中添加这个适配器.....

    import android.app.Activity;
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.Button;
    import android.widget.ImageButton;
    
    /**
     * Created by sushildlh on 10/14/16.
     */
    public class CustomAdapter extends BaseAdapter {
        private Context mContext;
    
        // Keep all Images in array
        public String[] text = {
                "1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "DONE"
        };
    
        // Constructor
        public CustomAdapter(Context c) {
            mContext = c;
        }
    
        public int getCount() {
            return 12;
        }
    
        public Object getItem(int position) {
            return null;
        }
    
        public long getItemId(int position) {
            return 0;
        }
    
        // create a new textView for each item referenced by the Adapter
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder = null;
            if (holder == null) {
                LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
                convertView = inflater.inflate(R.layout.item, parent, false);
                holder = new Holder();
                holder.button = (Button) convertView.findViewById(R.id.button);
                holder.image = (ImageButton) convertView.findViewById(R.id.image);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
    
            if (position != 9) {
                holder.image.setVisibility(View.GONE);
                holder.button.setText(text[position]);
            } else {
                holder.image.setImageResource(R.drawable.ic_backspace_black_24dp);    //change ic_backspace_black_24dp into to your image ......
                holder.button.setVisibility(View.GONE);
            }
    
            return convertView;
    
        }
    
        public static class Holder {
            Button button;
            ImageButton image;
        }
    
    
    }
    

    这是 item.xml 布局到您的布局文件夹中......

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button"
            android:layout_width="100dp"
            android:layout_height="60dp"
            android:gravity="center"
            android:background="@null"
            android:textSize="25dp"/>
    
        <ImageButton
            android:id="@+id/image"
            android:layout_width="100dp"
            android:layout_height="60dp"
            android:focusable="false"
            android:background="@null"
            android:gravity="center" />
    
    </LinearLayout>
    

    上面的代码给你这样的输出......

    注意:- 对于相同的视图模式,请始终使用 GridViewListView .....

    【讨论】:

    • 你是如何阻止GridView滚动的?
    • 你在哪里滚动答案?
    • GridViews滚动
    • 非虚拟机。由于某种原因,它不再滚动了。
    • 另外,您会注意到我的按钮的背景是白色的,而您却将您的按钮设置为透明 - 无需处理边框显示和动画不显示的问题。但是我的按钮必须是白色的,没有边框,并且带有默认动画。这就是我喜欢GridLayout 的原因,因为我得到了所有这些东西;忽略奇怪的“0”按钮行为。
    【解决方案4】:

    您可以使用 android:layout_weight 属性。

    【讨论】:

      【解决方案5】:

      看起来 GridLayout 为实际文本大小和一些默认填充保留了足够的高度,尽管 Done 按钮有 layout_height。

      【讨论】:

        【解决方案6】:

        改为使用“线性布局”构建它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-05
          • 1970-01-01
          • 2017-02-12
          • 1970-01-01
          • 2017-03-09
          • 1970-01-01
          相关资源
          最近更新 更多