在android中使用线性布局时的图像属性

发布于 2025-01-07 20:07:19 字数 1023 浏览 3 评论 0原文

您好,我正在尝试创建一个简单的 Android 页面,其中文本位于页面顶部,图像位于页面下方。我尝试过使用线性布局和相对布局,但我无法让图像显示在文本下方。图像要么位于文本旁边,要么位于左侧,要么位于屏幕右侧,或者当使用相对布局时,图像占据整个屏幕。

是否有任何图像属性可以实现此目的,如果可能的话,我不想使用已弃用的绝对布局。

这是我正在使用的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gripbckgrnd" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="140dp"
        android:layout_height="211dp"
        android:src="@drawable/grip1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="356dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="97dp"
        android:gravity="center_horizontal"
        android:text="@string/griptext"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

提前致谢

Hi Im trying to create a simple android page with text at the top of the page and an image below it. Ive tried using both linear and relative layouts but I just cannot get the image to display below the text. Either the image is next to the text either on the left or off the screen to the right, or when using a relative layout the image takes up the entire screen.

Is there any image properties that will allow to accomplish this, I do not want to use the deprecated absolute layout if possible.

Here is the code that I am working with:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gripbckgrnd" >


    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="140dp"
        android:layout_height="211dp"
        android:src="@drawable/grip1" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="356dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="97dp"
        android:gravity="center_horizontal"
        android:text="@string/griptext"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彻夜缠绵 2025-01-14 20:07:19

当使用LinearLayout时,你可以像这样指定方向属性:(

android:orientation="vertical"

默认是水平的,这会导致你的问题)

When using LinearLayout, you can specify the orientation-attribute like so:

android:orientation="vertical"

(The default is horizontal, which causes your problems)

狼性发作 2025-01-14 20:07:19

如果不需要,我建议您即使在 dp 中也不要指定 UI 组件。由于屏幕的尺寸可能会有微小的变化(例如,一些平板电脑的尺寸为 1280x800,一些平板电脑的尺寸为 1280x720),因此最好使用可以拉伸和稍微倾斜的规范,例如 match_parent、wrap_content 和 layout_weight。下面是使用其中一些属性的示例布局。

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/hello"
    />
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:src="@drawable/my_drawable"
    android:contentDescription="@string/describe_drawable"/>
</LinearLayout>

另外,您的示例将图像放在顶部。 :-)

I recommend you not specify your UI components even in dp, if you don't need to. Because screens can have minor variations in their dimensions (for example, some tablets are 1280x800 and some are 1280x720), it's better to use specifications that can stretch and give a bit, like match_parent, wrap_content, and layout_weight. Here's a sample layout that uses some of these attributes.

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:gravity="center"
    android:layout_weight="1"
    android:text="@string/hello"
    />
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_weight="2"
    android:src="@drawable/my_drawable"
    android:contentDescription="@string/describe_drawable"/>
</LinearLayout>

Also, your example puts the image on top. :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文