TableLayout击碎按钮 [英] TableLayout crushes button

查看:184
本文介绍了TableLayout击碎按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的问题,在Button旁边有一个TableLayout.这是XML:

I'm experiencing a strange issue with a TableLayout next to a Button. This is the XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">   
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:stretchColumns="*"              
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >    
        <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >    
                <TextView
                android:id="@+id/textView1"
                android:text="Column 1" />
                <Button
                    android:id="@+id/button1"
                    android:text="Column 2" />
        </TableRow>
    </TableLayout>
    <Button  
        android:id="@+id/button2"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content" 
        android:layout_weight="1"           
        android:text="Button 2" />                          
</LinearLayout> 

这就是我得到的:

这使我感到困惑.如您所见,ButtonTableLayoutlayout_weight均为1.因此,我希望它们在LinearLayout内具有相同的宽度,但是由于某些原因,TableLayout会占用更大的空间,然后将按钮一直推到右侧.

This is confusing me. As you can see, both the Button and the TableLayout have a layout_weight of 1. So I'd expect them to have the same width inside the LinearLayout but for some reason, the TableLayout takes up much more space and pushes the button all the way to the right.

请问如何根据layout_weight中设置的值在TableLayoutButton之间分配LinearLayout中的可用空间?

How can I have the additional space available in the LinearLayout distributed among the TableLayout and the Button according to the value set in layout_weight please?

请注意,我不想使用RelativeLayoutConstraintLayout.我专门在寻找LinearLayout解决方案.

Note that I don't want to use a RelativeLayout or ConstraintLayout. I'm specifically looking for a LinearLayout solution.

推荐答案

只需将其添加到您的父母即可.并使两个孩子的宽度<​​c14>

Just add this to your parent. and make width 0dp of both child

android:weightSum ="2"

android:weightSum="2"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2">

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/tableLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:stretchColumns="*">

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/textView1"
                android:text="Column 1" />

            <Button
                android:id="@+id/button1"
                android:text="Column 2" />
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button 2" />
</LinearLayout> 

这篇关于TableLayout击碎按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆