我创建了一个自定义视图,它是一条垂直线下的简单文本视图。该线位于视图的中心。这是XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vertBarLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/TV_date"
android:layout_alignRight="@+id/TV_date"
android:layout_alignEnd="@+id/TV_date"
>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/vertical_line"
android:layout_gravity="center_horizontal"/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1337"
android:id="@+id/TV_date"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
文本宽度可以改变,因此对齐的重要部分是垂直线。
如何从垂直线设置此视图的位置?
如果不能这样,是否有解决方法可以创建相同的视图?
答案 0 :(得分:0)
这将解决你希望的问题。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/vertBarLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/TV_date"
android:layout_alignRight="@+id/TV_date"
android:layout_alignEnd="@+id/TV_date"
>
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@drawable/vertical_line"
android:layout_gravity="center_horizontal"/>
</FrameLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="1337"
android:id="@+id/TV_date"
android:layout_centerInParent="true"
/>
</RelativeLayout>