Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何正确设计两边都有文本视图的线性布局?_Android_User Interface_Textview_Android Linearlayout_Android Layout Weight - Fatal编程技术网

Android 如何正确设计两边都有文本视图的线性布局?

Android 如何正确设计两边都有文本视图的线性布局?,android,user-interface,textview,android-linearlayout,android-layout-weight,Android,User Interface,Textview,Android Linearlayout,Android Layout Weight,我正在尝试编写一个线性布局,作为显示两个文本视图的列表视图的一部分。一个是银行账户名,另一个是余额。我想帐户名是在左侧的布局和平衡是在右侧 更具体地说,我希望余额右对齐,左边剩余的空间将用于帐户名,但我还没有弄清楚。我尝试使用布局权重,但它们不起作用 以下是我的代码: <LinearLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_he

我正在尝试编写一个
线性布局
,作为显示两个
文本视图的
列表视图
的一部分。一个是银行账户名,另一个是余额。我想帐户名是在左侧的布局和平衡是在右侧

更具体地说,我希望余额右对齐,左边剩余的空间将用于帐户名,但我还没有弄清楚。我尝试使用布局权重,但它们不起作用

以下是我的代码:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/accountNameTextView"
        android:layout_weight="2"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceSmall"
        android:id="@+id/accountBalanceTextView"
        android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/accountNameTextView"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceSmall"
        android:id="@+id/accountBalanceTextView"
        android:gravity="center_vertical"/>
</LinearLayout>


但实际情况是,两个文本视图都放在左侧,在右侧留下一堆空白。如何强制第二个文本视图位于右侧?

我一发布此问题,就找到了答案。感谢所有对此进行研究的人,但问题是LinearLayout宽度是包装内容,因此它会将两个文本视图相邻包装。将其更改为:

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

我能够使用布局权重来实现我想要的设计

我还稍微改变了布局权重,以下是我的:

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

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/accountNameTextView"
        android:layout_weight="2"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceSmall"
        android:id="@+id/accountBalanceTextView"
        android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:id="@+id/accountNameTextView"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:textAppearanceSmall"
        android:id="@+id/accountBalanceTextView"
        android:gravity="center_vertical"/>
</LinearLayout>

尝试以下代码:

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/accountBalanceTextView"
    android:textSize="30sp"
    android:id="@+id/accountNameTextView"
    />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:textAppearance="?android:textAppearanceSmall"
    android:id="@+id/accountBalanceTextView"
    />
</RelativeLayout>

我建议您改用
RelativeLayout
。下面是如何在
RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />

    <TextView
        android:id="@+id/gcmtv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="@string/hello_world" />

</RelativeLayout>


您为什么推荐相对布局?我认为线性布局最有意义,因为我只需要两个并排的文本视图。在您的情况下,没有具体的理由使用
RelativeLayout
。但是,如果您的
ListView
变得有点复杂,例如,如果您想在左侧下方显示另一个
TextView
,那么您的布局设计就会复杂得多。另一方面,在
RelativeLayout
中,这将非常简单。我也不知道:(可能是第三个试图吸引更多注意力的回答者。不过只是一个猜测。是的,我找到了答案,但我非常感谢你的投入。我不认为在这里使用RelativeLayout可以更轻松地完成我想要的任务,但我要设计的并不是真正建议使用RelativeLayout。在中使用它有好处吗stead?我认为您的代码中有一个小问题,因为您已经设置了这些文本视图的权重,一个文本视图将始终占据屏幕的66.66%,另一个将占据其余的33.33%,这永远不会像您所希望的那样,我不认为删除权重并设置左侧文本视图的宽度以匹配\u父视图和ri的宽度ght TextView包装内容将解决您的问题,因为我认为只有left TxtView可见,因此在这种情况下,最好的解决方案是使用RelativeLayout,您可以根据当前的要求完美地完成任务,希望我有所帮助:)是的,我也稍微改变了一下。请看我的答案的编辑,了解我的实际操作。gr8,这肯定会很有魅力!而且你的解决方案更优雅:)