【问题标题】:How to fix the width in Horizontal Linear Layout?如何固定水平线性布局中的宽度?
【发布时间】:2017-12-22 00:52:03
【问题描述】:

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

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

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

>

【问题讨论】:

  • 水平布局的子元素需要指定重力
  • 你能告诉我这个问题的重力值是多少吗?
  • 没有你的 XML 所有答案都只是猜测......显示一些代码
  • 请在此处粘贴您的 xml 文件,以便轻松解决您的问题@FaizaAhmad
  • 嗨。代码已粘贴@snachmsm

标签: android layout alignment


【解决方案1】:

试试这个,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】:

更好的布局实现应该是这样的

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

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

    • 连接TextView的宽度已经指定为100dp。
    • 96dp 只是一个例子,你可以设置你想要多少
    • 这是否意味着无法在线性布局中实现相同的效果?
    • 不,当然可以。我只是指出了这样做的首选方法。这可确保您从所有分辨率中获得相同的结果。
    • 这样,整个文本都显示出来了,但按钮的大小不断减小。
    【解决方案4】:

    您可以在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>
    

    【讨论】:

    • 在这种情况下它们的宽度应该是 0dp
    • 当我这样做时,布局会自动删除多余的 ahmad。
    • 这不是正确的答案...您的 XML 按钮将具有 1/6 的视图/布局/屏幕宽度 - 无法保证每个设备上的此空间足以显示整个按钮。除了在大屏幕上,特别是在水平方向上,按钮会非常大/宽
    • 另外,这解决了问题。但是视图移出了屏幕。如何解决这个问题?
    • @santalu 这只是举例,你应该添加组件的大小
    【解决方案5】:

    要实现顶部LinearLayout,您可以将layout_weight 用于TextView,值为1 以适应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>
    

    【讨论】:

    • @FaizaAhmad 有什么问题?
    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    • 1970-01-01
    • 2021-10-01
    • 2013-11-14
    相关资源
    最近更新 更多