如何删除添加到膨胀线性布局的视图之间不需要的空格?



我正在开发一个安卓应用程序,可以在两列的可滚动视图中显示一些电影。

一切都很好,除了我在行之间有很大的空间。

我已经尝试了很多在StackOverflow中找到的东西,比如:android:adjustViewBounds="true",将填充设置为零等等。

谁能给我开导?

这是应用程序的外观

如您所见,行之间有一个巨大的空间

我正在通过膨胀动态添加图像视图,我认为这就是问题所在。

这是我的代码:

package com.example.androidvidly
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.StringRequest
import com.android.volley.toolbox.Volley
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.squareup.picasso.Picasso
import org.json.JSONArray
import org.json.JSONObject
const val MOVIE_OBJECT_ID = "tokenlab.com.MOVIE_OBJECT_ID"
const val MOVIE_OBJECT_VOTES = "tokenlab.com.MOVIE_OBJECT_VOTES"
const val MOVIE_OBJECT_TITLE = "tokenlab.com.MOVIE_OBJECT_TITLE"
const val MOVIE_OBJECT_POSTER_URL = "tokenlab.com.MOVIE_OBJECT_POSTER_URL"
const val MOVIE_OBJECT_RELEASE_DATE = "tokenlab.com.MOVIE_OBJECT_RELEASE_DATE"
const val MOVIE_OBJECT_GENRES = "tokenlab.com.MOVIE_OBJECT_GENRES"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mQueue = Volley.newRequestQueue(this)
val url = "https://desafio-mobile.nyc3.digitaloceanspaces.com/movies" //API provided
//val url = "http://10.0.2.2:3000"// Node.js Backend
val gallery = findViewById<LinearLayout>(R.id.gallery)
val inflater = LayoutInflater.from(this)
var mAppName = findViewById<TextView>(R.id.appName)
mAppName.text="Vidly"
val gson = Gson()
var request = StringRequest(Request.Method.GET, url, Response.Listener { response ->
var moviesJSONArray = JSONArray(response)
for (i in 0 until moviesJSONArray.length()) {
var e : JSONObject = moviesJSONArray.getJSONObject(i)
//Initializing the object
val arrayStringType = object : TypeToken<ArrayList<String>>() {}.type
var genreListAux:ArrayList<String> = gson.fromJson(e.getString("genres"), arrayStringType)
var movieObject: Movie = Movie(e.getString("id").toInt(), e.getString("vote_average").toDouble(), e.getString("title"), e.getString("poster_url"), genreListAux, e.getString("release_date"))
if (i==0) {
var mImageView = findViewById<ImageView>(R.id.imageDisplay)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else if (i==1) {
var mImageView = findViewById<ImageView>(R.id.imageDisplay2)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else{
if(i.rem(2)==0) {
var view = inflater.inflate(R.layout.activity_main, gallery, false)
var mImageView = view.findViewById<ImageView>(R.id.imageDisplay)
mImageView.setTag(movieObject)
var height = gallery.height
view.minimumHeight=height
gallery.addView(view)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
else {
var view = gallery.getChildAt(gallery.childCount-1)
var mImageView = view.findViewById<ImageView>(R.id.imageDisplay2)
mImageView.setTag(movieObject)
loadImageFromUrl(movieObject.poster_url, mImageView)
mImageView.setPadding(0, 0, 0, 0);
}
}
}
}, Response.ErrorListener { error ->
error.printStackTrace()
})
mQueue.add(request)
}
fun showMore(view: View) {
val intent = Intent(this, MovieSelectedActivity::class.java).apply {
var movieObj : Movie = view.getTag() as Movie
putExtra(MOVIE_OBJECT_ID, movieObj.id)
putExtra(MOVIE_OBJECT_GENRES, movieObj.genres)
putExtra(MOVIE_OBJECT_POSTER_URL, movieObj.poster_url)
putExtra(MOVIE_OBJECT_RELEASE_DATE, movieObj.release_date)
putExtra(MOVIE_OBJECT_TITLE, movieObj.title)
putExtra(MOVIE_OBJECT_VOTES, movieObj.vote_average)
}
startActivity(intent)
}
fun loadImageFromUrl(url: String, mImageView: ImageView) {
Picasso.with(this).load(url).placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(
mImageView, null
)
}
}

下面是视图的 xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="10"
android:adjustViewBounds="true">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true">

<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="@style/TextAppearance.AppCompat.Body2"
android:textColor="#000000"
android:textSize="60dp"
android:adjustViewBounds="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentLeft="true"
android:adjustViewBounds="true">
<ImageView
android:id="@+id/imageDisplay"
android:layout_width="170dp"
android:layout_height="280dp"
android:clickable="true"
android:onClick="showMore"
android:adjustViewBounds="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:layout_alignParentRight="true"
android:adjustViewBounds="true">
<ImageView
android:id="@+id/imageDisplay2"
android:layout_width="170dp"
android:layout_height="280dp"
android:clickable="true"
android:onClick="showMore"
android:adjustViewBounds="true"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>


</ScrollView>

我发现问题出在我在主线性布局中设置的背景上。移除它后,巨大的空间消失了。

android:background="@drawable/background">

最新更新