【问题标题】:Image in layout is not completely on top, where I want it布局中的图像不完全位于我想要的位置
【发布时间】:2016-08-02 17:31:06
【问题描述】:

Edit, it's on top, now it's zoomed in

如果可能的话,我希望它在宽度方向上填满整个屏幕,图像完全在顶部,顶部和图像之间没有空间。这对我不起作用。

这是我的 main.xml 代码,可能完全错误,如果是这样,请告诉我。

This is how it looks in the emulator, I want it to fill the entire top side from left to right, completely on the top.

任何帮助将不胜感激,我对此很陌生,所以不要害怕告诉我我错了:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rodekruis.MainActivity">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true">
    <ImageView
         android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
        android:src="@drawable/rkz_logo"
         />

  </RelativeLayout>

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button9"
        android:layout_alignLeft="@+id/button10"
        android:layout_marginBottom="30dp"
        android:layout_gravity="top"
        android:clickable="true"
        android:text="Nieuws" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button10"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignTop="@+id/button8"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Afspraak   maken" />


        <Button
            android:id="@+id/button8"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Bezoek  tijden" />

        <Button
            android:id="@+id/button9"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Contact" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="20dp"
        android:orientation="horizontal">


        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="  Geef je mening!" />


        <Button
            android:id="@+id/button4"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text=" Route begeleiding" />

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignLeft="@+id/button5"
            android:layout_below="@+id/button8"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Specia-listen" />

    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/button5"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="BWC" />


        <Button
            android:id="@+id/button6"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:text="Agenda" />


        <Button
            android:id="@+id/button7"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:text="Praktische
 informatie" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • android:layout_alignParentTop="true" 在 ImageView 上并删除所有 android:padding*******="@dimen/activity_horizo​​ntal_margin"

标签: java android eclipse layout


【解决方案1】:

试试这个,

<ImageView
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_centerHorizontal="true"
   android:scaleType="centerCrop" 
   android:src="@drawable/rkz_logo"
  />

【讨论】:

  • 这使它从左到右整个屏幕,但完全放大我的图像。
  • 请您分享屏幕截图。以便我检查它的外观?
  • 添加到帖子中,顶部是图片的链接
  • 添加这个 android:scaleType="fitXY" 而不是 android:scaleType="centerCrop"
  • 好的,就这样吧。显然,图像被拉出,这使得字母看起来非常糟糕。这不是代码方面的,而只是图像方面的。您的代码完美无缺,谢谢! :) 不知道我将如何做到这一点,但我想我只需要创建一个自己的图像并记住我会扩大。会很难,但我要试试。谢谢!
【解决方案2】:

只需删除您的填充物。我希望它会有所帮助

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

【讨论】:

  • 我完全删除了这 4 行,没有任何改变。无论如何谢谢:)
  • 对不起,你也必须删除你的重力 android:gravity="center"
【解决方案3】:

删除布局根目录中的两行

android:gravity="center"
android:paddingTop="@dimen/activity_vertical_margin" 

试试这个。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
tools:context=".MainActivity">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher" />

<Button
    android:id="@+id/button2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_gravity="top"
    android:layout_marginBottom="30dp"
    android:clickable="true"
    android:text="Nieuws" />


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="20dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button10"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="  Afspraak   maken" />


    <Button
        android:id="@+id/button8"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="  Bezoek  tijden" />

    <Button
        android:id="@+id/button9"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:text="Contact" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginBottom="20dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">


    <Button
        android:id="@+id/button3"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="  Geef je mening!" />


    <Button
        android:id="@+id/button4"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text=" Route begeleiding" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_alignLeft="@+id/button5"
        android:layout_below="@+id/button8"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:text="Specia-listen" />

</LinearLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal">

    <Button
        android:id="@+id/button5"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="BWC" />


    <Button
        android:id="@+id/button6"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="Agenda" />


    <Button
        android:id="@+id/button7"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:text="Praktische informatie" />
</LinearLayout>

【讨论】:

  • 谢谢!这使得顶部的空间消失了,现在图像在顶部。现在它只需要填满整个宽度。不确定是否可以在不破坏图像的情况下这样做,因为它可能不是正确的图像。
【解决方案4】:

ImageView 的父级上的android:layout_alignParentTop="true" 被忽略,因为它的父级(根视图)不是RelativeLayout。尝试将根视图更改为RelativeLayout,并删除其填充(至少是顶部)。

【讨论】:

    猜你喜欢
    • 2018-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-06
    • 1970-01-01
    相关资源
    最近更新 更多