Jetpack UI撰写。如何创建FloatingActionButton?

时间:2019-10-24 18:44:17

标签: android kotlin floating-action-button android-jetpack-compose

我想使用class BillingListCell: UITableViewCell{ @IBOutlet weak var billWrapper: UIView! @IBOutlet weak var billTotal: UILabel! override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) // Configure the view for the selected state } } class BillingListHeaderCell: UITableViewCell{ @IBOutlet weak var titleLabel: UILabel! @IBOutlet weak var numberLabel: UILabel! @IBOutlet weak var statusButton: UIButton! func setExpanded() { statusButton.setImage(UIImage(systemName: "chevron.up"), for: .normal) } func setCollapsed() { statusButton.setImage(UIImage(systemName: "chevron.down"), for: .normal) } } class BillingListViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { @IBOutlet weak var billingListTableView: UITableView! var paymentArray: [String] = ["data","data2", "data3"] private let numberOfActualRowsForSection = 1 func numberOfSections(in tableView: UITableView) -> Int { return paymentArray.count } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { // First will always be header return false ? (1+numberOfActualRowsForSection) : 1 } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { if(indexPath.row == 0){ let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListHeaderCell", for: indexPath) as! BillingListHeaderCell cell.setCollapsed() return cell }else{ let cell = tableView.dequeueReusableCell(withIdentifier: "BillingListCell", for: indexPath) as! BillingListCell cell.billWrapper.layer.cornerRadius = 15 cell.billWrapper.layer.borderWidth = 1 cell.billWrapper.layer.borderColor = UIColor.blue.cgColor cell.billTotal.text = "1234" return cell } } 在活动中FloatingActionButton的右下角。我从来没有发现任何例子可以帮助我。预先感谢。

3 个答案:

答案 0 :(得分:3)

您可以使用可绘制图标创建FloatingActionButton下面的@Compose函数。

@Composable
fun MyFloatingActionButton() {
    Column(modifier = ExpandedHeight, crossAxisAlignment = CrossAxisAlignment.End, mainAxisAlignment = MainAxisAlignment.End ) {
        Column(modifier = Spacing(16.dp)) {
            val icon = +imageResource(R.drawable.ic_add_icon)
            FloatingActionButton(icon = icon, color = Color.Red, elevation = 8.dp)
        }
    }
}

答案 1 :(得分:3)

要创建FloatingActionButton,您可以使用类似以下内容的方法:

    setContent {
        MaterialTheme {

            val icon = imageFromResource(resources, R.drawable.ic_code)
            Center {
                val onClick: () -> Unit = { Log.e("Do something", "onClick") }
                Column(
                        ExpandedHeight,
                        mainAxisAlignment = MainAxisAlignment.SpaceEvenly,
                        crossAxisAlignment = CrossAxisAlignment.Center
                ) {
                    //FAB
                    FloatingActionButton(icon = icon, onClick = onClick)

                    //ExtendedFloatingActionButton
                    FloatingActionButton(text = "EXTENDED", onClick = onClick)
                    FloatingActionButton(icon = icon, text = "EXTENDED WITH ICON", onClick = onClick)
                }
            }
        }
    }

enter image description here

要在底部|右对齐:

val icon = imageFromResource(resources, R.drawable.baseline_code_black_24)
val onClick: () -> Unit = { Log.e("FABDemo", "onClick") }
Column(
      ExpandedHeight,
      mainAxisAlignment = MainAxisAlignment.End,
      crossAxisAlignment = CrossAxisAlignment.End
){
    Column(modifier = Spacing(16.dp)) {
        FloatingActionButton(icon = icon, onClick = onClick)
    }
}

答案 2 :(得分:1)

使用Scaffold

Scaffold 为最常见的顶级 Material 组件提供了插槽,例如 TopAppBar、BottomAppBar、FloatingActionButton 和 Drawer。通过使用 Scaffold,可以轻松确保这些组件正确定位并正确协同工作。

语法

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {/**/ },
        drawerContent = {/**/ },
        bottomBar = {/**/ },
        floatingActionButton = {/**/ },
        snackbarHost = {/**/ },
        content = {/**/ }
    )
}

示例

@Composable
fun HomeContent() {
    Scaffold(
        topBar = {
            TopAppBar(title = { Text("Title") })
        },
        floatingActionButton = {
            FloatingActionButton(
                onClick = { /*TODO*/ },
                backgroundColor = Color.Red,
                content = {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_add),
                        contentDescription = null,
                        tint = Color.White
                    )
                }
            )
        },
        content = {
            Surface(modifier = Modifier.padding(24.dp)) {
                Text(
                    text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
                    fontSize = 16.sp,
                )
            }
        }
    )
}

输出

enter image description here