Android 布局 - ScrollView 内的 ImageView - 图像前的空间

标签 android android-layout

我是 Android 新手,仍在学习如何完成工作的术语,并且对布局有疑问。

Trying to Achieve

我得到了什么(顶部的额外空间,黑色背景):

What I am getting

我想要一个带有顶栏的布局,其中包含一些按钮;中间,可滚动;和一个底栏,其中还包含按钮。我目前只实现了部分布局,但我有一个关于将 ImageView 放入 ScrollView 的问题。

这是我的布局 XML:

<RelativeLayout 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: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=".MainActivity" >

<Button
 android:id="@+id/btnPrevious"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="Previous" />

<Button
 android:id="@+id/btnCurrent"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/btnPrevious"
 android:text="Current" />

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/btnCurrent"
    android:fillViewport="true"
    >
    <RelativeLayout
        android:id="@+id/childOfScrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"   

        >
        <com.example.touch.TouchImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerInside"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="10sp"
            android:contentDescription="none"
            android:background="#000000"
            android:src="@drawable/ic_launcher" 
        />
   </RelativeLayout>

</ScrollView>

<Button
     android:id="@+id/btnExtra"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/scrollView"
     android:text="Extra"
 />

</RelativeLayout>

我正在使用 URL 动态设置图像,如下所示:
image = (ImageView) findViewById(R.id.image);
currentButton = (Button) findViewById(R.id.btnCurrent);
currentButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String address = ImageAddress.getImageAddress(0);
        Bitmap bm = getImageBitmap(address); //
        image.setImageBitmap(bm); // where 
    }
});

// get image bitmap later in my code
private Bitmap getImageBitmap(String url) {
    Bitmap bm = null;
    try {
        URL aURL = new URL(url);
        URLConnection conn = aURL.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
   } catch (IOException e) {
       Log.e("Problem", "Error getting bitmap", e);
   }
   return bm;
} 

快速总结一下,为什么我会在顶部(和底部)获得所有这些额外的引导空间(黑色背景)?

更新

Updated picture of layout
<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="wrap_content">

    <Button
     android:id="@+id/btnPrevious"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Previous" />

    <Button
     android:id="@+id/btnCurrent"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Current" />

</LinearLayout>

<ScrollView
    android:id="@+id/scrollView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:scrollbars="none"
    >

    <com.example.touch.TouchImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="centerInside"
        android:contentDescription="none"
        android:background="#000000"
        android:src="@drawable/ic_launcher"  />
</ScrollView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
         android:id="@+id/btnExtra"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Extra"
     />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="button2" />
</LinearLayout>
</LinearLayout>

最佳答案

要将 ImageView 边界调整为图像大小,包括:

android:adjustViewBounds="true"

这将删除在图像周围创建的边距。

关于Android 布局 - ScrollView 内的 ImageView - 图像前的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22521958/

相关文章:

java - 如何使用某些微调器的值设置从不同 fragment 打开 fragment ?

java - 如何以编程方式更改选择器内形状的颜色?

android - 制作一个显示在所有 fragment android中的按钮

java - Inflater 找不到布局 - Android

android - 修复了 TabLayout 和 Content 重叠的 AppBar

android - 无需点击即可自动打开通知

android - 什么 Android 布局允许包装 block /按钮 - 而不是奇怪的拉伸(stretch)?

android - 单击另一个 View 时 View 被隐藏

android - 为什么 Aviary 返回按比例缩小的图像?

Android 在 snapchat 上分享图片