You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Android Studio中用LinearLayout实现图片环绕文字布局

解决方案

要实现将布局置于屏幕顶部中央且仅使用LinearLayout的效果,只需对现有代码做两处关键调整:

  • 给外层LinearLayout添加android:gravity="top|center_horizontal"属性,让内部子控件沿顶部水平居中排列
  • 给ImageView添加顶部外边距,让内容和屏幕顶部保持合适间距(数值可根据需求调整)

修改后的完整代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E9C590"
    android:orientation="vertical"
    android:gravity="top|center_horizontal">

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="131dp"
        android:layout_height="144dp"
        android:layout_marginTop="24dp"
        app:srcCompat="@drawable/pizza_picture" />

    <TextView
        android:id="@+id/logoName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/oswald_medium"
        android:text="@string/pizzastic"
        android:textAllCaps="true"
        android:textColor="#682C20"
        android:textSize="34sp"
        android:textStyle="bold" />
</LinearLayout>

补充说明:

  • 外层的gravity属性统一控制子控件的排列规则,top|center_horizontal确保内容靠顶部且水平居中,无需再给每个子控件单独设置layout_gravity
  • layout_marginTop用于调整内容与屏幕顶部的距离,你可以根据视觉需求修改具体数值

内容的提问来源于stack exchange,提问作者Rebja A.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 15:22:41