繁体   English   中英

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

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

我创建了这个接口,使得表布局的最顶部一行具有3个使用layout_weight均匀分布的按钮。 但这就是我的最终目的:

在此处输入图片说明

这是我的代码:

      <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>

我没有添加其余的代码,因为它很长且不相关(我认为吗?)。 让我知道您是否还需要其余的东西,但我认为问题出在其中的布局? 你觉得错什么?

为什么要使用TableRow 您可以使用linearlayout创建它

使用以下代码

<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>

另一种方式

 <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>

尝试将宽度设置为0,而不要使用wrap_contentandroid:layout_width="0.0dip"

并检查以下链接以获取解释。

暂无
暂无

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

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