Android布局-ScrollView内的ImageView-图像前的空间 [英] Android Layouts - ImageView inside ScrollView - space before Image

查看:51
本文介绍了Android布局-ScrollView内的ImageView-图像前的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,仍然在学习如何做的术语,并对布局有疑问.

I am new to Android and still learning the lingo of how things are done and have a question about layouts.

我得到的东西(顶部多余的空间,黑色背景):

What I am getting (Extra space at the top, black background):

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

I would like to have a layout with a top bar, containing some buttons; a middle, which is scrollable; and a bottom bar, which also contains buttons. I've currently only implemented part of the layout, but I have a question on putting an ImageView inside a 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动态设置图片,例如:

And I am setting the Image dynamically with a URL, like such:

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;
} 

快速总结一下,为什么我要在顶部(和底部)得到所有这些多余的引线空间(黑色背景)?

To summarize quickly, why is it that I am getting all this extra lead space (the black background) at top (and bottom)?

更新

<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>

推荐答案

1,顶部是操作栏,要隐藏它,您可以使用NoActionBar主题或调用 getActionBar().hide();

1, the top area is the actionbar, to hide it you can use NoActionBar theme or call getActionBar().hide();

更新

2,您可以使用LinearLayout:

2, you can use LinearLayout:

<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/btn_download"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="button2" />
</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1">

    <ImageView
        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"
         />
</ScrollView>

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="button1" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:text="button2" />
</LinearLayout>
</LinearLayout>

活动:

public class TestActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        mDownloadButton = (Button) findViewById(R.id.btn_download);
        mDownloadButton.setOnClickListener(this);
        mImageView = (ImageView) findViewById(R.id.image);
    }

    private Button mDownloadButton;
    private ImageView mImageView;

    private final String ADDRESS =
        "http://images4.fanpop.com/image/photos/14700000/Beautifull-cat-cats-14749885-500-375.jpg";
    private static 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;
    }

    @Override
    public void onClick(View view) {
        new FetchBitmapTask().execute(ADDRESS);
    }

    private class FetchBitmapTask extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected void onPostExecute(final Bitmap bitmap) {
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   mImageView.setImageBitmap(bitmap);
               }
           });
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            return getImageBitmap(strings[0]);
        }
    }
}

这篇关于Android布局-ScrollView内的ImageView-图像前的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆