简体   繁体   English

Android Layout_Weight无法在TableLayout中正常工作?

[英]Android Layout_Weight not working properly in a TableLayout?

I created this interface such that, the top most row of the table layout has 3 equally distributed buttons using layout_weight. 我创建了这个接口,使得表布局的最顶部一行具有3个使用layout_weight均匀分布的按钮。 But this is what I end up with: 但这就是我的最终目的:

在此处输入图片说明

And this is my code: 这是我的代码:

      <RelativeLayout
            android:id="@+id/content_rel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ECECEC"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/blurImage"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <TableLayout
                    android:id="@+id/tableLayout1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

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

                        <ImageButton
                            android:id="@+id/btnHeartLike"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:src="@drawable/selector" />

                        <ImageButton
                            android:id="@+id/btnShare"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:src="@drawable/share39" />

                        <ImageButton
                            android:id="@+id/btnProductComment"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1"
                            android:clickable="true"
                            android:src="@drawable/comment_icon" />
                    </TableRow>

I didn't add the rest of the code since it's long and irrelevant (I think?). 我没有添加其余的代码,因为它很长且不相关(我认为吗?)。 Let me know if you need the rest as well but I think the problem is with the layout it is in? 让我知道您是否还需要其余的东西,但我认为问题出在其中的布局? What do you think is wrong? 你觉得错什么?

why you are using TableRow . 为什么要使用TableRow you can create it using linearlayout 您可以使用linearlayout创建它

use the following code 使用以下代码

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <Button
            android:id="@+id/button1"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button2"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />

        <Button
            android:id="@+id/button3"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Button" />
    </LinearLayout>

the another way 另一种方式

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <TableRow

                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <Button
                    android:id="@+id/button1"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button2"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />

                <Button
                    android:id="@+id/button3"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

Try setting the width to 0 instead of having wrap_content : android:layout_width="0.0dip" 尝试将宽度设置为0,而不要使用wrap_contentandroid:layout_width="0.0dip"

And check the following link for the explanation. 并检查以下链接以获取解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM