Java Android布局\u权重在TableLayout中工作不正常?
我创建了这个界面,使得表格布局的最上面一行有3个使用布局权重平均分布的按钮。但这就是我的结局: 这是我的代码:Java Android布局\u权重在TableLayout中工作不正常?,java,android,mobile,android-tablelayout,android-layout-weight,Java,Android,Mobile,Android Tablelayout,Android Layout Weight,我创建了这个界面,使得表格布局的最上面一行有3个使用布局权重平均分布的按钮。但这就是我的结局: 这是我的代码: <RelativeLayout android:id="@+id/content_rel" android:layout_width="match_parent" android:layout_height="match_parent" android:backgroun
<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\u内容
:android:layout\u width=“0.0dip”
并查看说明。我想相对布局不需要方向。只有线性布局需要它来定义它是垂直的还是水平的。另外,既然您只使用了3个图像按钮,为什么您要使用表格布局,而不使用水平线性布局,然后使布局_width=1,这将为您提供所需的效果。如果我不使用表格布局,则图像按钮会到处移位,并且不是在一个完美的行中,而且下面还有更多我没有发布的行堆栈溢出。你是说,让我的线性布局水平吗?因为我已经有了一个垂直的线性布局@KostasMatrixies。看看下面的@kamran答案,给他的答案加上水平方向不,我需要表格布局,先生:)下面还有其他项目,如果我不使用表格布局,它们就会到处乱窜。有没有一种方法可以使用表格布局,并且仍然在imagebuttons上使用布局权重?这看起来不错,但同样的问题仍然存在:(我认为这是因为我有多行。)