gpt4 book ai didi

java - 如何删除添加到膨胀的 LinearLayout 的 View 之间不需要的空格?

转载 作者:太空宇宙 更新时间:2023-11-04 09:11:28 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,以在两列的可 ScrollView 中显示一些电影。

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

我尝试了在 StackOverflow 中找到的许多方法,例如:android:adjustViewBounds="true"、将填充设置为零等。

谁能赐教一下吗?

Here is how the app is looking like

As you can see there is a huge space between rows

我通过 inflate 动态添加 imageView,我认为这就是问题所在。

这是我的代码:

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

这是 View 的 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"

关于java - 如何删除添加到膨胀的 LinearLayout 的 View 之间不需要的空格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619495/

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