所以,我有 2 個按鈕,我想通過使用偏好來交換它們,但是是的,我不能讓它們正確切換
這是我的偏好代碼
// In MainActivity.kt
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
// Buttons swaped.
} else {
// Buttons back to normal.
}
}
我再次嘗試讓它們交換,但我得到了一些像這樣的丑陋結果:
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
val posX = cleanBtn.x
val posY = cleanBtn.y
analyzeBtn.x = posX
analyzeBtn.y = posY
} else {
cleanBtn.setPadding(0, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 0, 0)
}
}
}
override fun onResume() {
super.onResume()
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition
val cleanBtn = findViewById<Button>(R.id.cleanBtn)
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn)
if (isButtonPositionFlipped) {
cleanBtn.setPadding(640, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 640, 0)
} else {
cleanBtn.setPadding(0, 0, 0, 0)
analyzeBtn.setPadding(0, 0, 0, 0)
}
}
}
我不得不提到我嘗試使用set.Margins但不作業
如何使用此方法交換它們?
uj5u.com熱心網友回復:
如果它們在 LinearLayout 中并且它們是該 LinearLayout 的唯一兩個子視圖,則可以像這樣顛倒子視圖的順序:
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toList()
removeAllViews()
for (child in children.asReversed()) {
addView(child)
}
}
如果它們需要不同大小的邊距,您也可以交換它們的布局引數:
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toList()
children[0].layoutParams = children[1].layoutParams
.also { children[1].layoutParams = children[0].layoutParams }
removeAllViews()
for (child in children.asReversed()) {
addView(child)
}
}
如果有兩個以上的子視圖,您可以在可變串列中搜索并交換它們,然后再將它們全部添加回父視圖。
findViewById<LinearLayout>(R.id.theParentLinearLayout).apply {
val children = children.toMutableList()
val buttonAIndex = children.indexOf(findViewById(R.id.buttonA))
val buttonBIndex = children.indexOf(findViewById(R.id.buttonB))
children[buttonAIndex] = children[buttonBIndex]
.also { children[buttonBIndex] = children[buttonAIndex] }
children[buttonAIndex].layoutParams = children[buttonBIndex].layoutParams
.also { children[buttonBIndex].layoutParams = children[buttonAIndex].layoutParams }
removeAllViews()
for (child in children) {
addView(child)
}
}
uj5u.com熱心網友回復:
嘗試使用RelativeLayout(或ConstraintLayout):
override fun onResume() {
super.onResume()
val cleanBtn = findViewById<Button>(R.id.cleanBtn);
val analyzeBtn = findViewById<Button>(R.id.analyzeBtn);
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val shouldFlipButtonPosition = sharedPreferences.getBoolean("buttonpositions", false)
var isButtonPositionFlipped = true;
if (isButtonPositionFlipped != shouldFlipButtonPosition) {
isButtonPositionFlipped = shouldFlipButtonPosition;
(cleanBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin =
dipToPixels(this, 100f);//100dp?
(analyzeBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin = 0;
} else {
(cleanBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin = 0;
(analyzeBtn.layoutParams as RelativeLayout.LayoutParams).leftMargin =
dipToPixels(this, 100f);//100dp?
}
}
private fun dipToPixels(context: Context, dipValue: Float): Int {
val metrics: DisplayMetrics = context.resources.displayMetrics
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics).toInt()
}
<?xml version="1.0" encoding="utf-8"?>
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context=".TestActivity">
<Button
android:id="@ id/cleanBtn"
android:layout_width="wrap_content"
android:text="111"
android:layout_height="wrap_content"></Button>
<Button
android:id="@ id/analyzeBtn"
android:layout_marginLeft="100dp"
android:layout_width="wrap_content"
android:text="222"
android:layout_height="wrap_content"></Button>
</RelativeLayout>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425246.html
