简体   繁体   English

对齐列表视图中的所有列

[英]Align all columns in list view

I was wondering how I can manage to align all columns in rows. 我想知道如何管理行中的所有列。 I am using a ListView , where I display a list of drugs for my game. 我正在使用ListView ,我在其中显示了我的游戏药物列表。 The columns are different sizes each row. 每列的列大小不同。 I would like to see that each column has the same width all down to the end of the list, but not have same width overall. 我希望看到每列都具有相同的宽度,直到列表的末尾,但总体上没有相同的宽度。

目前的实施

As you can see from the picture, the columns are not aligned. 从图中可以看出,列未对齐。 I draw the lines to make it even more obvious what I would like. 我绘制线条,使我更加明显的是我想要的。 You can also see that the buttons are to close to the other column. 您还可以看到按钮要靠近另一列。 With the arrow on the picture I wanted to make it clear that those buttons should be placed more to the end of the row. 使用图片上的箭头,我想清楚地说明这些按钮应该放在行的末尾。

My implementation uses the following styles: 我的实现使用以下样式:

<!-- List style for the drug sell list -->
<style name="traderY.List">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_alignParentLeft">true</item>
    <item name="android:layout_alignParentTop">true</item>
</style>

<!-- Header style for quantity column -->
<style name="traderY.HeaderQuantityText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.15</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#EDEFF0</item>
</style>

<!-- Header style for cost column -->
<style name="traderY.HeaderCostText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.20</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#EDEFF0</item>
</style>

<!-- Header style for name column -->
<style name="traderY.HeaderNameText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.45</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#EDEFF0</item>
</style>

<!-- Row style for quantity column -->
<style name="traderY.QuantityListText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.15</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#000000</item>
</style>

<!-- Row style for cost column -->
<style name="traderY.CostListText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.20</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#000000</item>
</style>

<!-- Row style for name column -->
<style name="traderY.NameListText">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.45</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:padding">4dip</item>
    <item name="android:layout_margin">4dip</item>
    <item name="android:textColor">#000000</item>
</style>

<!-- Row style for button column -->
<style name="traderY.ListButton" parent="@android:style/Widget.Button">
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">0.20</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textSize">15dip</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textColor">#FFFFFF</item>
    <item name="android:gravity">center</item>
    <item name="android:shadowColor">#000000</item>
    <item name="android:shadowDx">1</item>
    <item name="android:shadowDy">1</item>
    <item name="android:shadowRadius">0.6</item>
    <item name="android:background">@drawable/custom_btn_black_pearl</item>
    <item name="android:padding">4dip</item>
</style>

As you can see I set or I try to set the column width by using those two items: 正如您所看到的那样,我设置或尝试使用这两个项来设置列宽:

<item name="android:layout_width">0dip</item>
<item name="android:layout_weight">0.20</item>

I was reading that you can set the width by using weight. 我读到你可以用重量来设定宽度。 I basically used the same values I would use to set percentages. 我基本上使用了与设置百分比相同的值。 If I understand correctly, the weight value represents the ratio between other components. 如果我理解正确, weight值表示其他组件之间的比率。 I found this as a solution on a Stack Overflow question and I found it in several tutorials. 我发现这是Stack Overflow问题的解决方案,我在几个教程中找到了它。 One of the tutorials I found is this one here . 我找到的其中一个教程是这里的

To share more light, here are also the layouts of the of all the components, starting with the fragment which contains the list view. 为了分享更多光,这里还有所有组件的布局,从包含列表视图的片段开始。

Fragment layout 片段布局

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

  <TextView
    android:id="@+id/money"
    style="@style/traderY.TextCenter" />

  <TextView
    android:id="@+id/location"
    style="@style/traderY.TextCenter" />

  <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/junkie_icon"/>

  <ListView
    android:id="@+id/drug_list"
    style="@style/traderY.List"
    android:layout_weight="1"/>

  <TableRow
    style="@style/traderY.ButtonRow">

    <Button
        android:id="@+id/nothing"
        style="@style/traderY.ButtonCenter"
        android:text="@string/exit"/>

  </TableRow>

</LinearLayout>

Here is the header layout. 这是标题布局。 As you can see I omit the last column for buttons, because there is no need to display a header for it: 如您所见,我省略了按钮的最后一列,因为不需要为它显示标题:

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

  <TextView
    android:text="@string/header_quantity"
    style="@style/traderY.HeaderQuantityText" />

  <TextView
    android:text="@string/header_cost"
    style="@style/traderY.HeaderCostText" />

  <TextView
    android:text="@string/header_name"
    style="@style/traderY.HeaderNameText" />

</LinearLayout>

And here is the layout for each row I use: 这是我使用的每一行的布局:

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

  <TextView
    android:id="@+id/drug_quantity"
    style="@style/traderY.QuantityListText" />

  <TextView
    android:id="@+id/drug_cost"
    style="@style/traderY.CostListText" />

  <TextView
    android:id="@+id/drug_name"
    style="@style/traderY.NameListText" />

  <Button
    android:id="@+id/drug_sell_btn"
    android:text="@string/sell_drugs_action"
    style="@style/traderY.ListButton"/>

</LinearLayout>

Those are the style/layouts I use and I do not change any of those properties in the code. 这些是我使用的样式/布局,我不会在代码中更改任何属性。 So the problem can only be in my XML files. 所以问题只能出现在我的XML文件中。

Does anyone know what am I doing wrong here? 有谁知道我在这里做错了什么? Maybe my investigation is wrong and I can not do it. 也许我的调查是错误的,我不能这样做。 I would appreciate any help or comments to solve this problem. 我将不胜感激任何帮助或评论来解决这个问题。 If this is not even possible, then please let me know. 如果这不可能,请告诉我。

I found out that my solution was correct. 我发现我的解决方案是正确的。 The weight property did the job. weight属性完成了这项工作。 For some unknown reason gradle did not see the changes when it was building the app. 由于某些未知原因,gradle在构建应用程序时没有看到更改。 Or maybe it is the emulator. 或者也许它是模拟器。 This was not the first time something like this happened. 这不是第一次发生这样的事情。 I will have to investigate why this even occur. 我将不得不调查为什么会发生这种情况。

Increase weight of style="@style/traderY.QuantityListText". 增加style =“@ style / traderY.QuantityListText”的权重。 Make 0.2 or 0.3 制作0.2或0.3

Wrap your text views in a LinearLayout and set it to fill the parentView. 将文本视图包装在LinearLayout中并将其设置为填充parentView。 In this way all the buttons will be aligned to the right. 这样,所有按钮都将对齐到右侧。

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

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

    <TextView
      android:id="@+id/drug_quantity"
      style="@style/traderY.QuantityListText" />

   <TextView
     android:id="@+id/drug_cost"
     style="@style/traderY.CostListText" />

   <TextView
     android:id="@+id/drug_name"
     style="@style/traderY.NameListText" />
</LinearLayout>

<Button
  android:id="@+id/drug_sell_btn"
  android:text="@string/sell_drugs_action"
  style="@style/traderY.ListButton"/>

</LinearLayout>

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

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