我有三个 TextView。第一个和第三个是用不变的文本定义的。第二个,第一个和第三个之间的 TextView 通过输入获取值。如何在布局中的第一个和第三个 TextView 之间设置第二个 TextView?
<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/firsttext"
android:layout_above="@+id/thirdtext"
android:layout_alignLeft="@+id/firsttext"
android:layout_alignRight="@+id/thirdtext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
这不行!
<TextView
android:id="@+id/secondtext"
android:layout_width="282dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/firsttext"
android:layout_below="@+id/firsttext"
android:background="@drawable/border"
android:ellipsize="marquee"
android:lines="4"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
这也行不通
依赖项太多,请检查 xml 中是否出现循环依赖错误。根据您的要求,在不指定间距和其他 UI 细节的情况下,解决方案:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black" >
<TextView
android:id="@+id/firsttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="first"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firsttext"
android:background="@android:color/white"
android:text="second"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/thirdtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/secondtext"
android:background="@android:color/white"
android:text="third"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
尝试
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_toLeftOf="@id/thirdText"
android:layout_toRightOf="@id/firstText"
确保首先添加固定宽度的左右 TextView 并
android:layout_alignParentLeft="true"
和
android:layout_alignParentRight="true"
你的问题不清楚。但试试这个.. 如果您使用相对布局作为父级那么
<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/firsttext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
以水平方式显示三个文本视图。
<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firsttext"
android:background="@drawable/border"
android:scrollbars="vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
以垂直方式显示..
用
LinearLayout
和 height="wrap_content"
定义 width="fill_parent"
。
您可以通过两种方式解决您的问题。在您的
Textviews中添加
android:layout_below="@id/TextViewExample"
或使用 android:layout_marginTop="10dip"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black" >
<TextView
android:id="@+id/firsttext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:text="first"
android:layout_marginTop="10dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/secondtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/firsttext"
android:background="@android:color/white"
android:text="second"
android:layout_below="@id/firstText"
android:layout_marginTop="20dip"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/thirdtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/secondtext"
android:background="@android:color/white"
android:text="third"
android:layout_marginTop="30dip"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@id/secondtext"/>
</LinearLayout>
width="fill_parent" - 从 API 级别 8 开始不推荐使用此常量,并由 {@code match_parent} 取代。