UI--多行文本折叠展开效果

《代码里的世界》UI篇

用文字札记描绘自己 android学习之路

转载请保留出处 by Qiao
http://blog.csdn.net/qiaoidea/article/details/45568653

【导航】
- 单行文本水平触摸滑动效果 通过EditText实现TextView单行长文本水平滑动效果
- 多行文本折叠展开 自定义布局View实现多行文本折叠和展开


1.概述

  经常在APP中能看到有引用文章或大段博文的内容,他们的展示样式也有点儿意思,默认是折叠的,当你点击文章之后它会自动展开。再次点击他又会缩回去。
  网上有找到部分效果,感觉不是很满意。最后自己尝试用 自定义布局layout 写了个demo。比较简陋,不过可以用了。有这方面需求的朋友可以稍加改造下。如有更好的创意,也不妨分享一下。
  
效果图:
多行文本折叠展开


2.具体实现

  但从实现效果方面来看,只用简单定义必要view即可,后变为了方便扩展使用和挪用,又对整个布局进行封装,方便直接使用。

2.1 通过多个布局组合实现

  第一想法当然是用多个View组合来实现。那么久定义一个LinearLayout布局分别嵌套TextView和ImageView来做。
  大概步骤:
- 定义布局,垂直的线性LinearLayout布局、TextView和ImageView。 在layout中定义基本组件。
- 设置TextView的高度为指定行数*行高。 不使用maxLine的原因是maxLine会控制显示文本的行数,不方便后边使用动画展开全部内容。因此这里TextView的高度也因该为wrap_content。
- 给整个布局添加点击事件,绑定动画。 点击时,若TextView未展开则展开至其实际高度,imageView 旋转;否则回缩至 指定行数*行高 , imageView 旋转缩回。
  

开始编写代码:

  1.在xml中定义布局:

    <LinearLayout
        android:id="@+id/description_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:paddingLeft="12dip"
        android:paddingRight="12dip"
        android:paddingTop="5dip" >

        <TextView
            android:id="@+id/description_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:textSize="18dip" >
        </TextView>

        <ImageView
            android:id="@+id/expand_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:paddingBottom="5dip"
            android:paddingLeft="5dip"
            android:paddingRight="5dip"
            android:paddingTop="5dip"
            android:src="@drawable/text_ic_expand"
            android:visibility="gone" />
    </LinearLayout>

  2.首先在activity中定义并初始化这些view:

public class MainActivity extends Activity {
   
    TextView descriptionView;
    View layoutView ,expandView; 
  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
在Element UI中,要展开`el-table`表格的行,你可以使用`expand-row-keys`属性来控制展开的行。首先,你需要在表格的数据中添加一个属性用于标识是否展开该行,比如命名为`expanded`。然后,在`el-table`组件上添加`expand-row-keys`属性,并将其绑定到一个数组,用于存储展开的行的唯一标识。最后,在表格的列配置中,使用`scoped-slot`来自定义展开的内容。 下面是一个示例代码: ```html <template> <el-table :data="tableData" :expand-row-keys="expandedRows" @expand-change="handleExpandChange" > <!-- 自定义展开内容 --> <template v-slot="{ row }"> <el-form :inline="true"> <el-form-item label="姓名"> {{ row.name }} </el-form-item> <el-form-item label="年龄"> {{ row.age }} </el-form-item> <!-- 其他表单项 --> </el-form> </template> <!-- 表格列配置 --> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <!-- 其他列配置 --> </el-table> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, expanded: false }, { name: '李四', age: 20, expanded: false }, // 其他数据 ], expandedRows: [] // 存储展开的行的唯一标识 }; }, methods: { handleExpandChange(row, expanded) { // 当展开行变化时更新expandedRows数组 if (expanded) { this.expandedRows.push(row); // 将展开的行添加到数组中 } else { const index = this.expandedRows.findIndex(item => item === row); if (index > -1) { this.expandedRows.splice(index, 1); // 从数组中移除收起的行 } } } } }; </script> ``` 这是一个基本的示例,你可以根据你的实际需求自定义展开内容和其他列配置。请注意,你需要根据实际情况修改表格数据和列配置的属性名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值