Android 如何在relativelayout中显示两个文本视图
我正在玩一个包含两个文本视图的列表视图。现在看起来很糟糕 我希望文本(八、五等)在左边空白处开始。更重要的是,我希望右边的数字与左边的文字对齐。在中间 什么是最好的方法来对齐这两个项目在一个线与一些差距在中间。现在右边的文本在右上角 这是我的布局Android 如何在relativelayout中显示两个文本视图,android,android-layout,android-ui,Android,Android Layout,Android Ui,我正在玩一个包含两个文本视图的列表视图。现在看起来很糟糕 我希望文本(八、五等)在左边空白处开始。更重要的是,我希望右边的数字与左边的文字对齐。在中间 什么是最好的方法来对齐这两个项目在一个线与一些差距在中间。现在右边的文本在右上角 这是我的布局 <TextView android:id="@+id/num_name" android:layout_width="fill_parent" android:layout_height="wrap_content"
<TextView
android:id="@+id/num_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="0dip"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView android:id="@+id/num_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="13dip"
android:paddingLeft="13dip"
android:layout_alignBottom="@+id/cat_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
具有水平的
线性布局。将文本视图中的两个视图
放在其内部,且权重等于0.5。保持两个左对齐。您可以在整个布局上使用左_边距
<LinearLayout
android:orientation="horizontal"
android:margin_left="10dp"
>
<TextView
android:weight = "0.5"
/>
<TextView
android:weight = "0.5"
/>
</LinearLayout>
我只是在展示这个概念。您可以根据需要完成布局 要在左侧添加边距,请在XML文件中使用:
android:layout_marginLeft="10dp"
您可以使用以下选项将文本视图居中:
android:layout_centerHorizontal="true"
要将计数添加到名称右侧,请执行以下操作:
android:layout_toRightOf="@id/num_name"
我希望这有帮助
LinearLayour或RelativeLayout都可以在列表中使用。试试这个
它会起作用的。我在我的项目中尝试过这个
<RelativeLayout
<TextView
android:id="@+id/num_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="0dip"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView android:id="@+id/num_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/num_name"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="13dip"
android:paddingLeft="13dip"
android:layout_alignBottom="@+id/cat_id"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
试试这个
oh…..我的印象是,当布局中有多个项目时,不能使用linearlayout…我可以试试。@birdy是的,你可以。它最终有帮助吗?同时将左侧文本视图的android:layout\u width=“fill\u parent”更改为android:layout\u width=“wrap\u content”。您使用的父布局是什么??最好是线性布局
Try This
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="1"
android:orientation="horizontal" >
<TextView
android:id="@+id/num_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:gravity="left"
android:textColor="#000000"
android:textSize="16dip"
android:text="fgth"
android:textStyle="bold" />
<TextView
android:id="@+id/num_count"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:background="#9ed321"
android:gravity="right"
android:textColor="#ffffff"
android:textStyle="bold" />
</LinearLayout>