如何创建具有相等宽度的按钮?

如何解决如何创建具有相等宽度的按钮?

| 我想在屏幕中间显示三个按钮,并且三个按钮的宽度相同,尽管它们的文本标签长度不同。 只需添加三个带有不同长度文本标签的按钮,就会产生不同宽度的按钮。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"horizontal\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"wrap_content\"
    android:layout_gravity=\"center_vertical\"
    android:gravity=\"center\">
    <Button
        android:id=\"@+id/button_1\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"wrap_content\"
        android:text=\"ABCDEF\" />
    <Button
        android:id=\"@+id/button_2\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"wrap_content\"
        android:text=\"GHI\" />
    <Button
        android:id=\"@+id/button_3\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"wrap_content\"
        android:text=\"JKLM\" />
</LinearLayout>
默认的按钮宽度包装内容: - 将所有按钮上的layout_weight设置为1并将layout_width设置为0dip会使它们均匀拉伸以填充整个屏幕宽度。对于我想要的,这样的按钮实在太大了,尤其是在大屏幕上。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"horizontal\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"wrap_content\"
    android:layout_gravity=\"center_vertical\"
    android:gravity=\"center\">
    <Button
        android:id=\"@+id/button_1\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"ABCDEF\" />
    <Button
        android:id=\"@+id/button_2\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"GHI\" />
    <Button
        android:id=\"@+id/button_3\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"JKLM\" />
</LinearLayout>
布局权重1按钮填充屏幕宽度: - 在父级LinearLayout中为weightSum设置不同的值可用于阻止按钮填充整个屏幕,但是我不认为这是我要走的路,因为我不希望按钮占据一个大屏幕设备上的大部分屏幕。为了澄清,使用weightSum,例如,我可以将三个按钮设置为共同占据屏幕宽度的一半,在小屏幕上看起来不错,但是在大屏幕上,按钮仍将占据屏幕宽度的一半,并且按钮只会比我想要的大得多。也许最终的解决方案将是简单地为不同的屏幕使用不同的布局文件,但是我宁愿不走这条路。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"horizontal\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"wrap_content\"
    android:layout_gravity=\"center_vertical\"
    android:gravity=\"center\"
    android:weightSum=\"5\">
    <Button
        android:id=\"@+id/button_1\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"ABCDEF\" />
    <Button
        android:id=\"@+id/button_2\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"GHI\" />
    <Button
        android:id=\"@+id/button_3\"
        android:layout_height=\"fill_parent\"
        android:layout_width=\"0dip\"
        android:layout_weight=\"1\"
        android:text=\"JKLM\" />
</LinearLayout>
重量总和5小屏: 重量总和5大屏幕: - 我还使用TableLayout尝试了很多事情,但是没有什么比仅使用LinearLayout更好的了。 GridView使用起来非常笨拙,但是我还没有尝试过。 那么,如何创建具有相等宽度的按钮,最好是在仅具有必要宽度的情况下,才能使按钮内容最长的标签适合呢? 任何建议表示赞赏。 (我确实进行了搜索,发现这个问题问了很多遍,但是我发现的所有答案都没有解决我想要达到的目标。)     

解决方法

  也许最终的解决方案将是简单地为不同的屏幕使用不同的布局文件,但是我宁愿不走这条路。 许多程序员将使用
res/layout/
res/layout-large/
处理此类情况。在三个按钮的有限情况下,您可能有其他选择,但是通常用户界面并没有那么简单。   那么,如何创建具有相等宽度的按钮,最好是在仅具有必要宽度的情况下,才能使按钮内容最长的标签适合呢? 为了满足您的“最好”的要求,您需要为此创建一个自定义布局类。对于仪表板模式,这是与之相关的一种,您可以将其用作起点。     ,如果您预先知道最宽的按钮文字是什么,则可以使用
android:ems
属性将按钮设置为该宽度。
<Button
    android:id=\"@+id/my_button\"
    android:layout_width=\"wrap_content\"
    android:layout_height=\"wrap_content\"
    android:minEms=\"5\"
    android:text=\"@string/my_button\" />
它不是完美的,但这是我发现实现这种外观而又不间断地摆弄布局的最简单方法。     ,使用仪表板片段类 http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/ui/DashboardFragment.java     ,我这边只有一张纸条! 如果您需要设置在运行时添加的按钮(或其他任何具有简单代码更改的视图)的宽度(或高度),则可以使用下面的代码(不取决于方向):
public void AlignButtons(){
    llModules = (LinearLayout) findViewById(R.id.llModules);

    ViewTreeObserver viewTree = llModules.getViewTreeObserver();
    viewTree.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {

            int childcount = llModules.getChildCount();
            int maxWidth = 0; //int maxHeight = 0;

            String fontPath = \"fonts/Isabella-Decor.ttf\";
            Typeface tf = Typeface.createFromAsset(getAssets(),fontPath);

            for (int i = 0; i < childcount; i++) {
                LinearLayout v = (LinearLayout)llModules.getChildAt(i);
                View vv = v.getChildAt(0);
                if (vv instanceof Button) {
                    int width = vv.getMeasuredWidth();
                    maxWidth = (maxWidth > width) ? maxWidth : width;
                    //int height = vv.getMeasuredHeight();
                    //maxHeight = (maxHeight > height) ? maxHeight : height;
                }
            }
            for (int i = 0; i < childcount; i++) {
                LinearLayout v = (LinearLayout)llModules.getChildAt(i);
                View vv = v.getChildAt(0);
                if (vv instanceof Button) {
                    LayoutParams params = ((Button) vv).getLayoutParams();
                    params.width = maxWidth;
                    //params.height = maxHeight;
                    ((Button) vv).setLayoutParams(params);

                    // Applying font
                    ((Button) vv).setTypeface(tf);
                }
                vv = v.getChildAt(1);
                if (vv instanceof TextView) {
                    // Applying font
                    ((TextView) vv).setTypeface(tf);
                }
            }

            return true;
        }
    });
}
就我而言: llModule-一些布局,其中包含我们需要对齐的其他布局(
v.getChildAt(0);
)和其他布局(
v.getChildAt(1);
)。 LinearLayout v =(LinearLayout)llModules.getChildAt(i); -获取要对齐的按钮的布局。 其他部分很容易理解。 这段代码中的两个周期是完全相同的,但是我仍然无法想象如何将它们组合在一起(以加快执行时间)。     ,使用此正确的代码将为您提供帮助。
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"horizontal\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"wrap_content\"
    android:gravity=\"center\"
    android:weightSum=\"3\">

<Button
    android:id=\"@+id/button1\"
    android:layout_width=\"0dp\"
    android:layout_weight=\"1\"
    android:layout_height=\"wrap_content\"
    android:text=\"Get the Time\"
    android:onClick=\"showNewDate\" />
<Button
    android:id=\"@+id/button2\"
    android:layout_width=\"0dp\"
    android:layout_weight=\"1\"
    android:layout_height=\"wrap_content\"
    android:text=\"Get the Time\"
    android:onClick=\"showNewDate\" />
<Button
    android:id=\"@+id/button3\"
    android:layout_width=\"0dp\"
    android:layout_weight=\"1\"
    android:layout_height=\"wrap_content\"
    android:text=\"Get the Time\"
    android:onClick=\"showNewDate\" />
    ,将各个按钮保留在其自己的RelativeLayout下。
<LinearLayout
    android:layout_width=\"match_parent\"
    android:layout_height=\"wrap_content\"
    android:orientation=\"horizontal\">

    <RelativeLayout
        android:layout_width=\"wrap_content\"
        android:layout_height=\"match_parent\"
        android:layout_weight=\"1\">

        <Button
            android:layout_width=\"match_parent\"
            android:layout_height=\"wrap_content\"
            android:layout_alignParentStart=\"true\"
            android:text=\"Button1\"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width=\"wrap_content\"
        android:layout_height=\"match_parent\"
        android:layout_weight=\"1\">

        <Button
            android:layout_width=\"match_parent\"
            android:layout_height=\"wrap_content\"
            android:layout_alignParentEnd=\"true\"
            android:text=\"Button2\"/>
    </RelativeLayout>
</LinearLayout>
    ,读取该线程,然后进行一些反复试验,发现ѭ12是一个可接受的参数。现在,正如我所说,我偶然发现了这个问题,没有尝试使用它来解决您的三键方案。 自从您最初发布以来,情况很可能发生了变化。虽然我在为1.5版进行构建时尝试过此操作。那已经够老了... :-) 这是完整的
main.xml
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<LinearLayout
    xmlns:android=\"http://schemas.android.com/apk/res/android\"
    android:orientation=\"vertical\"
    android:layout_width=\"fill_parent\"
    android:layout_height=\"fill_parent\">

    <TextView
        android:id=\"@+id/dateText\"
        android:text=\"\\n\\nClick for Date\\n\\n\"
        android:layout_width=\"wrap_content\"
        android:layout_height=\"wrap_content\" />

    <Button
        android:id=\"@+id/button\"
        android:layout_width=\"180px\"
        android:layout_height=\"wrap_content\"
        android:layout_gravity=\"center_horizontal\"
        android:text=\"Get the Time\"
        android:onClick=\"showNewDate\" />

</LinearLayout>
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)&gt; insert overwrite table dwd_trade_cart_add_inc &gt; select data.id, &gt; data.user_id, &gt; data.course_id, &gt; date_format(
错误1 hive (edu)&gt; insert into huanhuan values(1,&#39;haoge&#39;); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive&gt; show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 &lt;configuration&gt; &lt;property&gt; &lt;name&gt;yarn.nodemanager.res