【问题标题】:setting up a tablelayout设置表格布局
【发布时间】:2017-10-03 07:16:56
【问题描述】:

我现在正在尝试构建一个列表应用程序,我需要在同一行中对齐按钮和文本,但由于我对 android studio 和 Java 非常陌生,我遇到了麻烦。我发现的任何指南都已过时 我的代码在下面

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:strechColumns="1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false">

        <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Job 1"
            />

    </TableRow>

    <ToggleButton
    android:id="@+id/button1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:onClick=""
    android:text="test text"
    />
</TableLayout>

任何帮助将不胜感激

【问题讨论】:

    标签: java android android-tablelayout


    【解决方案1】:

    在标签内试试下面的代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal"
      >
            <TextView
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:text="Job 1"
            />
            <ToggleButton
              android:id="@+id/button1"
              android:layout_alignParentBottom="true"
              android:layout_alignParentRight="true"
              android:onClick=""
              android:text="test text"
            />
         />
    

    【讨论】:

    • 谢谢你,但我想我会尝试使用表格的不同方法似乎是错误的方法来解决这个问题
    【解决方案2】:

    将您的 ToggleButton 放入 TableRow 会将您的项目放在同一行。

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1"
    android:strechColumns="1">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:baselineAligned="false">
    
            <TextView
    
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:text="Job 1"/>
    
            <ToggleButton
                android:id="@+id/button1"
                android:layout_alignParentBottom="true"
                android:layout_alignParentRight="true"
                android:text="test text"/>
    
        </TableRow>
    
    </TableLayout>
    

    如果您只需要一行中的两个元素,则可以使用 List/RecyclerView Adapter 实现相同的效果, 使用 List/RecyclerView 是比 TableView 简单的方法。在您的适配器中,使用以下 XML 代码进行项目迭代

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">
    
        <TextView    
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/toggleButton"
            android:layout_toStartOf="@+id/toggleButton"
            android:layout_alignParentleft="true"
            android:layout_alignParentStart="true"
            android:text="test "/>
    
        <ToggleButton
            android:id="@+id/toggleButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"/>
      </RelativeLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-11
      • 1970-01-01
      • 1970-01-01
      • 2011-10-28
      相关资源
      最近更新 更多