Dev*_*red 2 layout android match
我正在寻找布局系统的一些见解.是否有人知道Android布局系统中允许对等 View元素(意味着在同一容器中组合在一起的视图)匹配其高度和宽度属性而不创建插页式容器以控制其大小的方法?
例如:布局有两个TextView元素.第一个应该始终将宽度包装到其文本内容,但第二个应该匹配第一个的宽度.WORKS的解决方案是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
但是,我必须为两个视图创建一个特殊的容器,以确保它们都匹配第一个文本的宽度; 为层次结构添加不必要的复杂性 我想知道的是,如果有一个布局属性我可以用来做这样的事情:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_widthMatchPeer="@id/textOne" <!-- An attribute like this -->
android:layout_height="wrap_content"
android:layout_below="@id/textOne" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
创建一个硬尺寸作为宽度会破坏让主要元素包裹的目的.有没有人知道的技术,或者我错过的属性,才能实现这一目标?可以在任何标准布局中,我只是选择RelativeLayout作为一个例子.
另外,如果元素不在彼此附近怎么办?
干杯.
我认为RelativeLayout班级可以做你想做的事:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textTwo"
android:layout_alignLeft="@id/textOne"
android:layout_alignRight="@id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textOne" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1549 次 |
| 最近记录: |