如何修复水平线性布局中的宽度?

时间:2017-07-17 13:00:35

标签: android layout alignment

如何修复水平线性布局中每个元素的宽度。如图所示?

我已使用代码更新了问题。帮助将不胜感激。谢谢!

enter image description here

<

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/l1"
android:orientation="horizontal"
android:layout_height="87dp"
android:layout_width="match_parent"
android:paddingBottom="9dp"
android:paddingLeft="19dp"
android:paddingRight="19dp"
android:paddingTop="9dp"
android:gravity="center"
>

<ImageView
    android:id="@+id/fbprofile_picture"
    android:layout_width="63dp"
    android:layout_height="63dp"
    android:src="@drawable/ic_logo_new"/>

<TextView
    android:id="@+id/fbprofile_name"
    android:layout_width="wrap_content"
    android:layout_height="18dp"
    android:fontFamily="sans-serif-condensed"
    android:paddingLeft="17dp"
    android:paddingRight="17dp"
    android:text="Faiza Zainab Ahmad " />

<TextView
    android:id="@+id/btn_fbfriends_connect"
    android:layout_width="100dp"
    android:layout_height="40dp"
    android:layout_weight="2.31"
    android:background="@color/ThemeLightBlue"
    android:gravity="center"
    android:padding="8dp"
    android:text="CONNECT"
    android:textColor="#ffffff"
    android:textSize="16sp"
    />

</LinearLayout>

&GT;

5 个答案:

答案 0 :(得分:2)

更好的布局实现就像这样

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

使用权重将布局划分为比例宽度。

答案 1 :(得分:2)

试试这个,ImageView和Button将保持其大小,TextView将填充其余空间。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:scaleType="fitCenter"
        android:src="@drawable/my_image" />


    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:layout_weight="1"
        android:text="Some text" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="The Button" />

</LinearLayout>

答案 2 :(得分:1)

您可以静态设置图片的宽度。即:android:layout_width="96dp"

在这种情况下,最好使用 RelativeLayout 而不是 LinearLayout 权重。即:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?listPreferredItemHeight"
    android:paddingBottom="8dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="8dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="72dp"
        android:layout_height="72dp"
        android:layout_centerVertical="true"
        android:src="@mipmap/ic_launcher"/>

    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_toLeftOf="@+id/button"
        android:layout_toRightOf="@+id/image"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"/>

</RelativeLayout>

答案 3 :(得分:1)

您可以在LinearLayout中添加weightSum,并将layout_weight添加到该LinearLayout的子项中,例如:

<LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:weightSum="3"
      android:orientation="horizontal">
    <ImageView
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:src="@drawable/ic_group_white_24dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"/>
    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"/>

  </LinearLayout>

答案 4 :(得分:0)

要获得最高LinearLayout,您可以layout_weight使用值为1的TextView来填充ImageViewButton之间的空格

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

    <ImageView
        android:layout_width="80dp"
        android:layout_height="80dp" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
相关问题