【问题标题】:Can't adjust weights in linear layout - android无法在线性布局中调整权重 - android
【发布时间】:2015-04-15 06:35:13
【问题描述】:

我已经搜索过,但无法找出问题所在。

我想做和上图一模一样的。我在“Day Teams Score Status”中使用了垂直的线性布局 然后对于包含上述标题值的内容,我使用了列表视图。

我所取得的是标题“Day Teams Score Status”用空格显示,但内容(在单独的 .xml 文件中)没有用空格显示,所以我得到的是:

列表视图的适配器xml文件是:

adapter_my_matches.xml

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


    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Date"
        android:id="@+id/textViewDate"
        android:layout_weight="0.1"
        android:layout_gravity="center_vertical" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="0.4">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Teams"
            android:id="@+id/textViewTeams1"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Teams"
            android:id="@+id/textViewTeams2"/>
    </LinearLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Score"
        android:id="@+id/textViewScore"
        android:layout_weight="0.1"
        android:layout_gravity="center_vertical" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Status"
        android:id="@+id/textViewStatus"
        android:layout_weight="0.2"
        android:layout_gravity="center_vertical" />
</LinearLayout>

【问题讨论】:

  • 是的,这是一个正在运行的代码,但给出了意想不到的布局。
  • @MuhammadRehanQadri,请检查我的答案并告诉我

标签: android xml listview layout android-linearlayout


【解决方案1】:

将 layout_weight 赋予父布局并赋予子元素权重,并确保 layout_weight 应为其所有子元素的总权重之和。将孩子的宽度设为 warp_content。

【讨论】:

  • 我应该设置它的宽度来换行内容。你回答的最后一句话含糊不清。请澄清。
【解决方案2】:

使用这个:

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

    <TextView
        android:id="@+id/textViewDate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:text="Date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewTeams1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Teams"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/textViewTeams2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Teams"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <TextView
        android:id="@+id/textViewScore"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:text="Score"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewStatus"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="2"
        android:text="Status"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

【讨论】:

  • 同样的问题。您将在 xml 的 android studio 预览版上运行您的代码。但是当您在模拟器或真实设备上运行时,它不会按预期显示。遇到同样的问题。
  • 完全正确。实际上是每列之间的差距。
  • 对于空视图设置空字符串
  • 从每个textview的xml中删除了文本,但没有效果。
【解决方案3】:

我认为这是您正在寻找的布局。如果有任何问题,请告诉我。这里是我为样品设置的重量,您可以根据需要进行更改。

<?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="vertical"

    >
    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum= "6">
         <TextView
        android:layout_width="0dp"

        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Date"
        android:id="@+id/textViewDate"
        android:layout_weight="1.2"
        android:gravity="center" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Teams"
            android:id="@+id/textViewTeams1"
            android:gravity="center"
            android:layout_weight="2"/>


        <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Score"
        android:id="@+id/textViewScore"
        android:layout_weight="1.4"
        android:gravity="center"
         />

       <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Status"
        android:id="@+id/textViewStatus"
        android:layout_weight="1.4"
        android:gravity="center"
         />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum= "6">
         <TextView
        android:layout_width="0dp"

        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="12 Feb"
        android:id="@+id/textViewDate1"
        android:layout_weight="1.2"
        android:gravity="center" />
        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="IND VS PAK"
            android:id="@+id/textViewTeams11"
            android:gravity="center"
            android:layout_weight="2"/>


        <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="256"
        android:id="@+id/textViewScore1"
        android:layout_weight="1.4"
        android:gravity="center"
        />

       <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="WIN"
        android:id="@+id/textViewStatus1"
        android:layout_weight="1.4"
        android:gravity="center"
         />
    </LinearLayout>

</LinearLayout>

【讨论】:

  • 感谢兄弟,它成功了!你能提一下我的代码的问题吗?
  • 我检查了问题兄弟,如果文本很大,它会显示在下一行:( .
  • 好的,谢谢,你能告诉我我的代码有什么问题吗?
  • 在您的代码中 1. 您必须提及 Weightsum,2. 根据需要给出权重值,3. 始终 android:layout_width = "0" 使用基于宽度的权重。如果工作+1,你能接受答案吗:P
  • 您在做正确的工作并且最好地在线搜索,了解您使用的数据是什么,并了解更多相关信息。
【解决方案4】:

将此添加到您的线性布局代码中

android:weightSum="0.8" 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多