Я пытаюсь создать ListView
, который также подойдет для экранов планшетов. Я использую реализацию SwipeListView
, поэтому каждый элемент ListView
является RelativeLayout
.
Итак, я хочу создать элемент ListView
, который сохранит соотношение сторон из приложения для смартфона. Я попытался использовать следующую реализацию:
public class FixedAspectRatioFrameLayout extends FrameLayout
{
private int mAspectRatioWidth;
private int mAspectRatioHeight;
public FixedAspectRatioFrameLayout(Context context)
{
super(context);
}
public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
Init(context, attrs);
}
public FixedAspectRatioFrameLayout(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
Init(context, attrs);
}
private void Init(Context context, AttributeSet attrs)
{
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FixedAspectRatioFrameLayout);
mAspectRatioWidth = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioWidth, 4);
mAspectRatioHeight = a.getInt(R.styleable.FixedAspectRatioFrameLayout_aspectRatioHeight, 3);
a.recycle();
}
// **overrides**
@Override protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
{
int originalWidth = MeasureSpec.getSize(widthMeasureSpec);
int originalHeight = MeasureSpec.getSize(heightMeasureSpec);
int calculatedHeight = originalWidth * mAspectRatioHeight / mAspectRatioWidth;
int finalWidth, finalHeight;
if (calculatedHeight > originalHeight)
{
finalWidth = originalHeight * mAspectRatioWidth / mAspectRatioHeight;
finalHeight = originalHeight;
}
else
{
finalWidth = originalWidth;
finalHeight = calculatedHeight;
}
super.onMeasure(
MeasureSpec.makeMeasureSpec(finalWidth, MeasureSpec.EXACTLY),
MeasureSpec.makeMeasureSpec(finalHeight, MeasureSpec.EXACTLY));
}
}
Из этого принятого ответа: Просмотр с фиксированным соотношением сторон, но изменение его на RelativeLayout
. Но по какой-то причине я получаю "cannot be resolved error.."
в следующем коде:
R.styleable.FixedAspectRatioFrameLayout
R.styleable.FixedAspectRatioFrameLayout_aspectRatioWidth
R.styleable.FixedAspectRatioFrameLayout_aspectRatioHeight
<declare-styleable></declare-styleable>
в файлattrs.xml
. См. kevindion.com/2011/01/custom-xml- attribute-for-android-widgets, чтобы узнать больше. - person Vicky Chijwani   schedule 17.02.2014