Abr*_*asa 5 layout android center margin vertical-alignment
在Android的RelativeLayout中,我们可以使用以下代码在屏幕中央设置textView:
<TextView
android:text="This is TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
Run Code Online (Sandbox Code Playgroud)
结果:
top
-
-
-
-
This is TextView (center vertical)
-
-
-
-
bottom
Run Code Online (Sandbox Code Playgroud)
但我需要textView有点底部,我尝试添加marginTop但似乎在使用后layout_centerVertical=true变得不可能.有解决方案吗
预期结果(略微下调):
top
-
-
-
-
-
-
This is TextView (center vertical slight to bottom)
-
-
bottom
Run Code Online (Sandbox Code Playgroud)
试试这个:
尝试使用RelativeLayout,它可以使用重量轻松完成您的要求:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<TextView
android:id="@id/dummy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_below="@id/dummy"
android:layout_marginTop="10dp"
android:text="This is TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
尝试使用LinearLayout,它可以使用重量轻松完成您的要求:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical">
<TextView
android:text="This is TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
为什么要在父视图中添加嵌套视图?
使用paddingTop属性.
例:
<RelativeLayout
android:id="@+id/rlParentView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingTop="10dp"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
希望这会帮助你.
| 归档时间: |
|
| 查看次数: |
15356 次 |
| 最近记录: |