需求需要我们实现一个价格列表,其中要求名称项左对齐,价格项右对齐。
查了一会儿才知道咋解决该问题,记录下来,以资来者。
示例代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="显卡价格:" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100.0元" />
</LinearLayout>
核心代码
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
这块说明使用权重,名称项比重为1,后面价格项只能占据够展示的长度,其余都会被名称项占据。从而达到,两个组件两端对齐的效果。