簡體   English   中英

從約束布局循環設置視圖子項的重力

[英]Set gravity of view children from loop of constraintlayout

當我通過ConstraintLayout循環訪問動態創建為TextView時,它們將作為View返回,而沒有setGravity

有誰知道一種設置這些視圖的重力的方法( TextView / child的重力,而不是布局的重力)嗎?

for (int i = 0;i < constraintLayout.getChildCount();i++) {
    final View child = constraintLayout.getChildAt(i);
    Object tag = child.getTag(R.id.tagId);
    if(tag != null) {
        //child.setGravity(Gravity.START); // Doesn't work...
    } else {} }

我也沒有通過ConstraintLayout.LayoutParamsConstraintSet設置Gravity任何選項。

另外,我嘗試了final TextView child = (TextView) constraintLayout.getChildAt(i); :但是,這導致了致命異常: java.lang.ClassCastException:android.widget.ImageView無法轉換為android.widget.TextView 注意:子級是textviews,不是imageviews ...

首先將視圖轉換為TextView ,然后調用setGravity

final View child = constraintLayout.getChildAt(i);
Object tag = child.getTag(R.id.tagId);
if (tag != null) {
    if (child instanceof TextView) {
        TextView tv = (TextView) child;
        tv.setGravity(Gravity.START);
    }
} else {}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM