добавить представление в customview и изменить его размер - PullRequest
0 голосов
/ 24 июня 2019

У меня есть пользовательский вид, который расширяется от FrameLayout:

public class FanView extends FrameLayout {

private static final String TAG = FanView.class.getSimpleName();

private List<FanItem> fanItems = new ArrayList<>();
private float openRatio = 0f;

public FanView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

private void init() {
    setStaticTransformationsEnabled(true);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    Log.d(TAG, "onLayout(" + changed + ", " + left + ", "  + top + ", " + right + ", " + bottom + ")");
    super.onLayout(changed, left, top, right, bottom);
    Log.d(TAG, "End onLayout");
}

@Override
protected boolean getChildStaticTransformation(View child, Transformation t) {
    final float index = getChildCount() - indexOfChild(child) - 1;
    final float height = child.getHeight();
    Matrix matrix = t.getMatrix();
    matrix.setTranslate((float) (-index * 1.1 * (height/2) * openRatio), (float) (index * height * openRatio * 1.2));
    return true;
}

public void setOpenRatio(float r) {
    this.openRatio = r;
    final int childCount = getChildCount();
    for (int i = 0; i < childCount; i++) {
        View view = getChildAt(i);
        view.invalidate();
    }
    invalidate();
}

public void setFanItems(List<FanItem> fanItems) {
    this.fanItems = fanItems;
    removeAllViewsInLayout();
    for (int i = 0; i < fanItems.size(); i++) {
        int fanItemIndex = fanItems.size() - i - 1;
        FanItem fanItem = fanItems.get(fanItemIndex);
        View fanView = inflate(getContext(),
                fanItemIndex == 0 ? R.layout.fan_item_header : R.layout.fan_item, null);
        TextView textView = (TextView) fanView.findViewById(R.id.fan_view_item_title);
        textView.setText(fanItem.getTitle());
        addViewInLayout(fanView, i,
                new LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));
    }
    invalidate();
}

}

в макете XML я использую этот пользовательский вид с шириной и высотой wrap_content. когда я добавляю вид в этот пользовательский вид, не показывать этот вид потому что пользовательский макет не меняет его размер. как сказать пользовательскому виду изменить его размер

Ответы [ 2 ]

0 голосов
/ 24 июня 2019

Пожалуйста, укажите действительный размер, чтобы сначала надуть предметы.Дайте хотя бы один элемент, чтобы получить размер после инфляции макета.Вы можете изменить размер элементов, используя параметры макета.

  <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="schemas.android.com/apk/res/android" 
    android:background="@drawable/fan_item_background" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:textDirection="rtl">
    <TextView
        android:id="@+id/fan_view_item_title"
        android:layout_height="200dp" 
        android:layout_width="200dp" 
        android:text="ersfsdf"
        android:textAlignment="textStart"
        android:textColor="#ffffff"/>
</FrameLayout>

Чтобы динамически изменить размер, попробуйте что-то вроде этого.

// Gets linearlayout
FrameLayout layout = findViewById(R.id.framelaut);// or any layout
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = 100;
params.width = 100;
layout.setLayoutParams(params);
0 голосов
/ 24 июня 2019

Ваш текущий код, вероятно, нуждается в другом конструкторе

public FanView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
...