我可以包含六个按钮并正确显示为。
现在我想在可滚动视图中包含六个以上的按钮,但我无法处理它以匹配可用空间,创建2行(如果是纵向,则为列)。
你能提供一些方法来实现这个目标吗?
参考文献:
我有一个使用以下代码创建的复合按钮:
public class ImageButtonText extends RelativeLayout {
ImageButton button;
TextView label;
Holder holder;
private void init() {
LayoutInflater li = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
li.inflate(R.layout.big_button, this, true);
button = (ImageButton) findViewById(R.id.button);
label = (TextView) findViewById(R.id.label);
/*initialise button and label*/
}
}
以及以下xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="@dimen/component_margin">
<ImageButton
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/text_box"
android:scaleType="fitCenter"/>
<TextView
android:id="@+id/label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_margin="@dimen/component_margin"
android:gravity="center"
android:text="Sample"
android:textSize="@dimen/text_size"/>
</RelativeLayout>
最后使用:
添加到主布局<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageButtonText
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
custom:buttonBackground="@drawable/states_green"
android:src="@drawable/ic_safe_call"
android:contentDescription="@string/btn1_info"
android:text="@string/btn1text"
android:textColor="@color/text_green" />
<ImageButtonText
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
custom:buttonBackground="@drawable/states_red"
android:src="@drawable/ic_private_call"
android:contentDescription="@string/btn2_info"
android:text="@string/btn2text"
android:textColor="@color/text_red" />
</LinearLayout>
备注
也可能是一种不同的方法。我的最终目标是显示多个按钮(每个按钮包含拉伸图像和底部对齐文本),如果是纵向,则显示两行;如果是横向,则显示三列。所有这些都包含在可滚动的视图中。
更新 我用RecyclerView解决了:D谢谢大家
答案 0 :(得分:1)
一种简单而动态的方法是创建一个自定义的按钮列表,并将其放入带有2个Colums的GridView中。行。
我为我的导航所做的这个适配器有一个图标和一个文本,也许它可以帮助你。图标位于左侧,您可以在此行中读取:holder.textView.setCompoundDrawablesWithIntrinsicBounds(item.get_icon(),0,0,0);第二个是文本上方的图标。
无论如何,我认为你需要一个自定义布局,你可以轻松创建一个带有Relative或LinearLayout的新xml文件,并将ImageView和TextView放入其中,并在构造函数layoutResourceId中作为参数提供。
您可以在此布局xml文件中定义的listItem的高度。
您可以为横向和纵向配置不同的GridView
在纵向模式下,将使用layout-port /的布局
public class NavigationAdapter extends ArrayAdapter<NavItem> {
List<NavItem> data;
Context context;
int layoutResourceId;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
NavHolder holder;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new NavHolder();
holder.textView = (TextView)row.findViewById(android.R.id.text1);
row.setTag(holder);
}
else
{
holder = (NavHolder) row.getTag();
}
NavItem item = data.get(position);
holder.textView.setText(item.get_title());
holder.textView.setCompoundDrawablesWithIntrinsicBounds(item.get_icon(), 0, 0, 0);
//holder.textView.setCompoundDrawablePadding(10);
return row;
}
public NavigationAdapter(Context context, int layoutResourceId, List<NavItem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
static class NavHolder
{
TextView textView;
}
}