【问题标题】:Best practice for layout two textviews on a row在一行上布局两个文本视图的最佳实践
【发布时间】:2012-12-08 17:25:03
【问题描述】:

我想在我的 xml 布局上有很多 textviews ,每两个在一行上,我是这样的:

<?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="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Name: " />

        <TextView
            android:id="@+id/playertvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textSize="20dp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nationality: " />

        <TextView
            android:id="@+id/playertvNationality"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/red"
            android:textSize="20dp" />
    </LinearLayout>

</LinearLayout>

但是为每两个文本视图创建一个新的线性布局似乎不是一个好方法,请问还有其他方法吗?谢谢

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    试试GridLayout。这里是official Google doc,这里是example

    【讨论】:

    • 我的 android 上没有 GridLayout,是我错了还是我必须安装一些东西?
    • @totti roma 它是在 API 14 (Android 4.0) 中引入的。但是 v7 支持库中有一个 backport,因此您可以将它用于 API 7+(Android 2.1+)。看看这个问题:stackoverflow.com/questions/10278358/…
    • 第一个 +1 给你,第二个我应该在哪里下载这个库?我在 eclipse 上使用 2.2 模拟器,第三,如果我和我一起工作很好,当我将 adk 移动到我的手机时,那个库会随之移动吗?还是我也应该把它下载到照片上?
    • 我打开 sdk 时一切都安装好了,请你告诉我在哪里可以找到 griplayout?
    【解决方案2】:

    我相信,出于您使用布局(可能是一种形式)的目的,您开发它的方式非常好。在这种情况下,我认为 GridView 或任何 AdapterView 都不适合您的要求。

    您可以使用具有适当权重的LinearLayoutRelativeLayout

    【讨论】:

    • 谢谢,但为什么你不认为 gridlayout 不好? BTY我希望有另一个没有gridlayout的解决方案,因为我似乎必须下载库:(
    • 对不起,我把GridLayout和GridView搞混了。我认为 GridLayout 适合您的情况,我现在已经支持 deville 的回答 :) GridLayout 有官方的 v7 兼容性包。如果你还不想用,RelativeLayout 就好了。
    • 但是如何下载 griplayout?我打开了sdk,一切都安装好了
    • 当我构建项目时,我将目标平台设置为 2.2,bty 我已经安装了 4.3 平台,所以如果我将目标平台更改为 4.3 并且运行良好,那么当我将 apk 移动到我的手机(2.2)它可以工作还是我仍然必须将这些库安装到我的手机上?
    • 尝试添加支持库。右键项目,Android Tools > Add support library 然后使用
    【解决方案3】:

    您可以使用 TableLayout 或 GridView,作为 LinearLayout 的替代。

    1. TableLayout 将允许您以表格格式排列文本视图,如果文本视图的数量非常少且固定,我建议使用 TableLayout。

    2. GridView 允许您以 Grid 风格布局您的组件,它是一个适配器视图,这意味着通过 GridView 我们可以重用视图。它在一定程度上增加了复杂性,因此当没有文本视图时它很有用。

    【讨论】:

      【解决方案4】:

      使用一种相对布局并使用 android:layout_toLeftOf="", android:layout_toRightOf="" 机器人:layout_below="" android:layout_above=""

      【讨论】:

        猜你喜欢
        • 2012-10-30
        • 1970-01-01
        • 1970-01-01
        • 2019-02-13
        • 1970-01-01
        • 2011-10-26
        • 1970-01-01
        • 2016-01-02
        • 1970-01-01
        相关资源
        最近更新 更多