我有下面的代码,我在其中构建了一张“卡片”并向其添加了Switch
和button
,我需要button
正好在交换机下,怎么做!
val swithy = Switch(this).apply {
text = "active"
isChecked = true
id = View.generateViewId()
}
val mcard = CardView(this).apply {
background = getDrawable(R.drawable.card_background)
radius = 12F
setContentPadding(25, 25, 25, 25)
setCardBackgroundColor(Color.LTGRAY)
cardElevation = 8F
maxCardElevation = 12F
addView(swithy, RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT).apply {
RelativeLayout.ALIGN_PARENT_BOTTOM
RelativeLayout.ALIGN_PARENT_LEFT
})
addView(Button(this.context).apply {
text = "click me"
}, LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT).apply {
/* addRule(RelativeLayout.BELOW, swithy.id); */
// How to make this?! it works in Java, how to make it in Kotlin
})
}
card_background.xml
是:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:name="custom_view"
android:id="@+id/custom_view">
<stroke
android:width="1dp"
android:color="#c2f2f2f2" />
<solid
android:color="#FFFFFFFF"
/>
<corners
android:bottomRightRadius="2dp"
android:bottomLeftRadius="2dp"
android:topLeftRadius="2dp"
android:topRightRadius="2dp" />
</shape>
答案 0 :(得分:0)
我能够使用ConstraintLayout
管理我的要求,我在这里发布我的答案,可能有些人觉得它有用:
在MainActivity.kt
我将自定义元素称为:
main_layout.addView(Cardy(this))
我的自定义元素Cardy.kt
是:
import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.v4.content.ContextCompat
import android.widget.Button
import oryx.kortex.locateme.R
import android.graphics.Color
import android.support.constraint.ConstraintSet
import android.widget.EditText
import android.util.TypedValue
class Cardy : ConstraintLayout {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val r = resources
val px = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200f,
r.displayMetrics).toInt()
val myEditText = EditText(context).apply {
id = ConstraintLayout.generateViewId()
width = px
}
val myButton = Button(context).apply {
text = "Press Me"
setBackgroundColor(Color.BLUE)
id = ConstraintLayout.generateViewId()
}
this.apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
background = ContextCompat.getDrawable(context, R.drawable.card)
addView(myButton)
addView(myEditText)
}
val set = ConstraintSet().apply {
constrainHeight(myButton.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myButton.id, ConstraintSet.WRAP_CONTENT)
connect(myButton.id, ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0)
connect(myButton.id, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0)
connect(myButton.id, ConstraintSet.TOP,
ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0)
connect(myButton.id, ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0)
constrainHeight(myEditText.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myEditText.id, ConstraintSet.WRAP_CONTENT)
connect(myEditText.id, ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0)
connect(myEditText.id, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0)
connect(myEditText.id, ConstraintSet.BOTTOM,
myButton.id, ConstraintSet.TOP, 70)
}
set.applyTo(this)
}
}
输出是:
备注强>:
这部分是以元素horizontal
:
connect(myButton.id, ConstraintSet.LEFT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT, 0)
connect(myButton.id, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT, 0)
这部分是以元素vertically
:
connect(myButton.id, ConstraintSet.TOP,
ConstraintSet.PARENT_ID, ConstraintSet.TOP, 0)
connect(myButton.id, ConstraintSet.BOTTOM,
ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM, 0)
要让两个元素在同一行中,请在代码下面执行以下任务:
val set = ConstraintSet().apply {
constrainHeight(myEditText.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myEditText.id, ConstraintSet.WRAP_CONTENT)
constrainHeight(myButton.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myButton.id, ConstraintSet.WRAP_CONTENT)
connect(myButton.id, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,0)
}
答案 1 :(得分:0)
可以使用以下方法完成:
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.BELOW, ELEMENT.id)
}
以下是我的完整运行代码:
val card1 = Switchy(this).apply {
id = ConstraintLayout.generateViewId()
}
val card2 = Cardy(this).apply {
id = ConstraintLayout.generateViewId()
}
main_layout.addView(card1)
main_layout.addView(card2,
RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.BELOW, card1.id)
})
main_layout.addView(Settings(this),
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.BELOW, card2.id)
}
)
Switchy
的位置:
class Switchy : RelativeLayout {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val colorOn = -0xcdc1ba
val trackStates = ColorStateList(
arrayOf(intArrayOf(-android.R.attr.state_checked), intArrayOf()),
intArrayOf(Color.LTGRAY, colorOn)
)
this.apply {
layoutParams = android.widget.RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
addView(Switch(context).apply {
background = ContextCompat.getDrawable(context, R.drawable.card)
text = "active or not is it ?"
isChecked = true
showText = true
textOn = "Active"
textOff = "Not Active"
trackTintList = trackStates
thumbTintList = trackStates
}, LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT))
}
}
}
Cardy
是:
class Cardy : ConstraintLayout {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val r = resources
val px = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 200f,
r.displayMetrics).toInt()
val myEditText = EditText(context).apply {
id = ConstraintLayout.generateViewId()
width = px
}
val myImageButton = ImageButton(context).apply({
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
})
val myButton = Button(context).apply {
text = "Press Me"
setBackgroundColor(Color.BLUE)
id = ConstraintLayout.generateViewId()
}
this.apply {
layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
background = ContextCompat.getDrawable(context, R.drawable.card)
addView(myEditText)
// addView(myButton)
addView(myImageButton)
}
val set = ConstraintSet().apply {
constrainHeight(myEditText.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myEditText.id, ConstraintSet.WRAP_CONTENT)
constrainHeight(myImageButton.id, ConstraintSet.WRAP_CONTENT)
constrainWidth(myImageButton.id, ConstraintSet.WRAP_CONTENT)
connect(myImageButton.id, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,0)
}
set.applyTo(this)
}
}
答案 2 :(得分:0)
LinearLayout也可以是一个选项,在代码下方设置EditText
ImageButton
:
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v7.widget.CardView
import android.widget.*
import android.widget.LinearLayout
class RespondTo : CardView {
constructor(context: Context) : super(context) {
init(context)
}
private fun init(context: Context) {
val parent = LinearLayout(context)
parent.apply {
layoutParams = LinearLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT, 1.0f).apply {
orientation = LinearLayout.HORIZONTAL
addView(EditText(context).apply {
id = generateViewId()
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.9f).apply {
}
})
addView(ImageButton(context).apply({
layoutParams = LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 0.1f)
background = null
setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_save_black_24px))
id = generateViewId()
layoutParams = RelativeLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT).apply {
addRule(RelativeLayout.ALIGN_PARENT_RIGHT)
// addRule(RelativeLayout.LEFT_OF, myImageButton.id)
}
}))
}
}
this.addView(parent)
}
}