java响应式UI GridView布局
我在其他网站上做过调查,对于如何解决我的问题,我得到了不同的回答。事实上,问题是我正在尝试制作一个响应性的gridView布局,以显示25个带数字的文本视图。我很难按照我想要的方式建造它,但我得到的是:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:orientation="vertical"
安卓:weightSum="10"
安卓:layout_gravity="center"
安卓:gravity="center"
tools:context="com.example.hristodraganov.bingo.MainActivity"
tools:showIn="@layout/activity_main">
<RelativeLayout
安卓:id="@+id/relLayout"
安卓:layout_width="match_parent"
安卓:layout_height="0dp"
安卓:layout_weight="4">
</RelativeLayout>
<GridView
安卓:paddingTop="8dp"
安卓:id="@+id/gridview"
安卓:background="#000"
安卓:layout_width="match_parent"
安卓:layout_weight="6"
安卓:layout_height="0dp"
安卓:gravity="center"
安卓:columnWidth="70dp"
安卓:numColumns="5"
安卓:verticalSpacing="10dp"
安卓:horizontalSpacing="1dp"
安卓:stretchMode="spacingWidth"
/>
这是在MainActivity上使用的布局。我使用文本视图项将其充气,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="#d3d3d3"
安卓:gravity="center">
<com.example.****.****.SquareView xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:id="@+id/cell"
安卓:textColor="#D0583B"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:gravity="center"
安卓:textSize="30sp">
</com.example.****.****.SquareView>
有一个BaseAdapter类的扩展,用于使用TextView扩展网格:
public class GridAdapter extends BaseAdapter {
private final Context mContext;
private String[] numbers;
public GridAdapter(Context context, String[] numbers) {
this.mContext = context;
this.numbers = numbers;
}
@Override
public int getCount() {
return numbers.length;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(mContext);
gridView = inflater.inflate(R.layout.textview_layout, null);
TextView textView = (TextView) gridView.findViewById(R.id.cell);
textView.setText(numbers[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
}
另外,我在这个SquareView类中强制对textView进行平方处理:
public class SquareView extends 安卓.support.v7.widget.AppCompatTextView {
public SquareView(Context context) {
super(context);
}
public SquareView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}}
布局现在已经很好了,下面是我用来构建它的大小的图像Nexus 5X。它看起来不错,但当我改为4“-Nexus S时,整个最后一行都在屏幕下方的某个地方。同样,在5“-Nexus 5中,最后一行略微可见。5.2英寸到6.0英寸以上的布局非常适合。因此,我的问题是,在这种情况下,我应该做些什么,使布局适合小屏幕,而不必为它们制作重复的布局。(注意:我被告知BaseAdapter实现将修复布局的响应性)。你知道我能做什么吗?(很抱歉发了这么长的短信。)
# 1 楼答案
因为有很多不同的Android设备,所以最好的选择是将布局封装在
ScrollView
中另一种选择是去掉权重,让顶部
RelativeLayout
在GridView
占用了它所需的所有空间后增长/收缩。这在较小的屏幕上或在风景中可能仍然不起作用```
“```”