在MaterialButton的文本顶部放置图标

时间:2020-07-04 14:10:18

标签: android android-layout material-design android-button material-components-android

我想将图标放在materialButton上方,如下所示:enter image description here

但是我在MaterialButton中找不到任何属性来将app:icon的重力设置为顶部。 我只能将其设置为开始或结束。 有没有办法在不将Vertical LinearLayout与ImageView和TextView一起使用的情况下设置创建类似按钮的方法?

2 个答案:

答案 0 :(得分:1)

您可以为此使用createTable = () => { const rows = []; const { tableData } = this.state; tableData.forEach((row, i) => { const cell = []; for (let idx = 0; idx < 8; idx++) { cell.push( <td contenteditable='true' onChange={e => this.handleChangeTable(e, i, idx)} //to incorporate event with other params key={`${i}-${idx}`} id={idx} //cell no ></td> ); } rows.push( <tr key={i} id={i}> <img id={i} onClick={() => this.removeRow(i)} src={Image} alt={cell} /> {cell} </tr> ); }); return rows; }; 属性

注意:这不仅限于您可以使用的MaterialButton 在TextViews和其他视图上也可以

示例代码:

removeRow = (rowIndex) => {
    const{ tableData } = this.state;

    tableData.splice(rowIndex, 1);

    this.setState({ tableData });
    console.log(`removed ${rowIndex}`);
};

答案 1 :(得分:1)

您可以使用 iconGravity="textTop"

    <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.TextButton"
        app:icon="@drawable/ic_add_24px"
        app:iconGravity="textTop" />

enter image description here

注意:至少需要版本 1.3.0-alpha02