我在獲得我想要使用的結果時遇到了一些問題RelativeLayout,還想知道我是否也應該同時使用LinearLayout。這是我的代碼:
package me.soft.myapp
import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.ScrollView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
class MainActivity : AppCompatActivity() {
lateinit private var constraintLayout: ConstraintLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
constraintLayout = this.findViewById(R.id.activity_main)
val relatLayOut = RelativeLayout(this)
relatLayOut.id = View.generateViewId()
val rllyLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT)
relatLayOut.layoutParams = rllyLOP
constraintLayout.addView(relatLayOut)
// We define 2 colored stripes and one scroll view.
val bannerOne = stripe("Hello wonderful world of yours ...",
Color.rgb(0xEE,0xEE,0x22))
val bannerTwo = stripe("The purpose of our lives is to be happy.",
Color.rgb(0x22,0xEE,0xEE))
val scrolZone = scrollBlock(longNumberString(1000))
// We add them to the global layout.
relatLayOut.addView(bannerOne)
relatLayOut.addView(bannerTwo)
relatLayOut.addView(scrolZone)
// Set the layout and order of the components:
val layoutOption = 2//1
if (layoutOption == 1) {
setViewBelow(bannerTwo,bannerOne)
setViewBelow(scrolZone,bannerTwo)
}
if (layoutOption == 2) {
setViewBelow(scrolZone, bannerOne)
setViewBelow(bannerTwo, scrolZone)
}
}
fun Int.dpToPixels(context: Context):Float = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,this.toFloat(),context.resources.displayMetrics
)
fun stripe(display:String, bgc: Int): RelativeLayout {
val lnlyLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
val lnlyOut = RelativeLayout(this)
lnlyOut.id = View.generateViewId()
lnlyOut.gravity = Gravity.CENTER
lnlyOut.layoutParams = lnlyLOP
lnlyOut.setBackgroundColor(bgc)
val theLabel = TextView(this)
theLabel.text = display
theLabel.setTextSize(TypedValue.COMPLEX_UNIT_PX, 25.dpToPixels(this))
theLabel.gravity = Gravity.CENTER
lnlyOut.addView(theLabel)
return lnlyOut
} /* End of stripe */
fun scrollBlock(display:String): ScrollView {
val scrolViw = ScrollView(this)
scrolViw.id = View.generateViewId()
val rllyLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
scrolViw.layoutParams = rllyLOP
val theLabel = TextView(this)
theLabel.text = display
scrolViw.addView(theLabel)
return scrolViw
} /* End of scrollBlock */
fun setViewBelow(theView: View, referView: View) { // To set theView below referView.
val memoLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
memoLOP.addRule(RelativeLayout.BELOW, referView.id)
theView.layoutParams = memoLOP
} /* End of setViewBelow */
fun longNumberString(n:Int): String { // To make a long string of numbers.
if (n<0) return ""
var resultStr = "0"
for (i in 1..n) {resultStr = "-$i"}
return resultStr
} /* End of longNumberString */
}
當我使用layoutOption = 1時,此代碼按預期作業。但不是當我使用layoutOption = 2時。
這是我在設備上得到的layoutOption = 1:

這是我在設備上得到的layoutOption = 2:

但在這里我期望layoutOption = 2:

顯然我對layoutOption = 2想要的結果做錯了。
任何人都可以看到錯誤在哪里?
這是 XML 檔案,以防萬一:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@ id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
uj5u.com熱心網友回復:
由于您使用的是 RelativeLayout,因此您可以執行以下操作來實作您想要的結果:
一個。使用LayoutParams#alignParentBottom使您的 bannerTwo 底部與父底部對齊
灣。使用LayoutParams#layout_above使您的 ScrollView 底部成為 bannerTwo 的頂部
C。同樣,使用LayoutParams#layout_below使您的 ScrollView Top 成為 BannerOne 的底部
這將鎖定兩個橫幅之間的 ScrollView 的大小,同時使bannerOne 和bannerTwo 在螢屏上可見。希望有幫助。
uj5u.com熱心網友回復:
感謝Pratik K的回答中提供的提示;這是我最終從帖子中的代碼解決問題的方法。我替換了以下代碼行:
if (layoutOption == 2) {
setViewBelow(scrolZone, bannerOne)
setViewBelow(bannerTwo, scrolZone)
}
由以下人:
if (layoutOtion == 2) {
stickViewAtBottom(bannerTwo)
setViewBetween(scrolZone,bannerOne,bannerTwo)
}
其中兩個新函式stickViewAtBottom()和setViewBetween()定義為:
fun stickViewAtBottom(theView: View) { // To set theView at the bottom of its parent.
val memoLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
val parentID = (theView.parent as View).id
memoLOP.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, parentID)
theView.layoutParams = memoLOP
} /* End of stickViewAtBottom */
fun setViewBetween(theView: View, aboveView: View, beneathView: View) {
val memoLOP = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT)
memoLOP.addRule(RelativeLayout.BELOW, aboveView.id)
memoLOP.addRule(RelativeLayout.ABOVE, beneathView.id)
theView.layoutParams = memoLOP
} /* End of setViewBetween */
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/504669.html
