【问题标题】:Android: TextView and Button on Same Line in ExpandableListViewAndroid:ExpandableListView 中同一行的 TextView 和 Button
【发布时间】:2023-03-07 23:45:01
【问题描述】:

所以我有一个 ExpandableListView,基本上我有一些文本(人名),然后我想要在同一行上有一个按钮(如果可能,朝向右侧)。目前,我的 XML 文件中的这段代码在一行上有文本,然后在下一行有按钮,我已经玩过它,但不知道如何在一行上得到它。

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="55dip"
android:orientation="horizontal"
android:weightSum="1" >

<TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_weight="0.8"
android:textSize="11dp"
android:text="Checkout"
android:id="@+id/checkout" />

</LinearLayout>

【问题讨论】:

    标签: android xml android-layout button expandablelistview


    【解决方案1】:

    你有

    android:orientation="vertical"
    

    在您的父级LinearLayout 中(自然地)从上到下堆叠元素。删除该行(因为horizontalLinearLayout 的默认方向)。然后您需要使用weightmargin 将其定位到您想要的位置。

    另一种选择是将您的LinearLayout 替换为RelativeLayout。这将允许您在 Button 上使用 android:alignParentRight="true" 将其放在屏幕的右侧(RelativeLayout 默认将 Views 放在左上角)。

    【讨论】:

      【解决方案2】:

      这样就可以了。

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="55dip"
          android:orientation="horizontal" // Edited here
          android:weightSum="1" >  // here
       
      <TextView
          android:id="@+id/lblListItem"
          android:layout_width="match_parent"  //and here
          android:layout_height="35dp"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:gravity="center_vertical"
          android:layout_weight="0.2" // here
          android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
      
      <Button
          style="?android:attr/buttonStyleSmall"
          android:layout_width="match_parent" // and here
          android:layout_height="wrap_content"
          android:focusable="false"
          android:focusableInTouchMode="false"
          android:layout_weight="0.8" // and here
          android:textSize="12dp"
          android:text="Checkout"
          android:id="@+id/checkout" />
      
      </LinearLayout>
      

      使用 weightsum 时要确保两件事:

      1. 如果方向是水平的,则要显示在一行中的所有子视图的宽度将需要具有“match_parent”和一些 layout_weight 的值。

      2. 0.2 将提供父级水平宽度的 80%,0.8 将提供 20%。

      【讨论】:

      • '所有要连续显示的子视图都需要有 "match_parent"' 通常它们是 "0dp"
      • 是的,0dp 会更有效率。因为它会导致属性被忽略。
      • 感谢您和其他所有人的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-16
      • 2011-04-04
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多