gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:23:03 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com