如何在 Activity 中滚动两列按钮
How to scroll two-columns buttons in Activity
我可以包含六个按钮并正确显示为 。
现在我想在可滚动视图中包含六个以上的按钮,但我无法处理它以匹配可用的 space,创建 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 谢谢大家
一种简单而动态的方法是创建一个自定义按钮列表,然后将其放入具有 2 个列的 GridView 中。行。
我为我的导航做的这个适配器有一个图标和一个文本,也许它对你有帮助。该图标位于左侧,您可以在这一行中看到:holder.textView.setCompoundDrawablesWithIntrinsicBounds(item.get_icon(), 0, 0, 0);第二个是文字上方的图标。
无论如何,我认为您需要一个自定义布局,您可以使用 Relative 或 LinearLayout 轻松创建一个新的 xml 文件,并将 ImageView 和 TextView 放入其中并作为参数提供构造函数 layoutResourceId.
您可以在此布局 xml 文件中定义的列表项的高度。
您可以为横向和纵向配置不同的 GridView
- 在横向模式下,将使用 layout-land/ 中的布局
在纵向模式下,将使用来自 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;
}
}
我可以包含六个按钮并正确显示为
现在我想在可滚动视图中包含六个以上的按钮,但我无法处理它以匹配可用的 space,创建 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 谢谢大家
一种简单而动态的方法是创建一个自定义按钮列表,然后将其放入具有 2 个列的 GridView 中。行。
我为我的导航做的这个适配器有一个图标和一个文本,也许它对你有帮助。该图标位于左侧,您可以在这一行中看到:holder.textView.setCompoundDrawablesWithIntrinsicBounds(item.get_icon(), 0, 0, 0);第二个是文字上方的图标。
无论如何,我认为您需要一个自定义布局,您可以使用 Relative 或 LinearLayout 轻松创建一个新的 xml 文件,并将 ImageView 和 TextView 放入其中并作为参数提供构造函数 layoutResourceId.
您可以在此布局 xml 文件中定义的列表项的高度。
您可以为横向和纵向配置不同的 GridView
- 在横向模式下,将使用 layout-land/ 中的布局
在纵向模式下,将使用来自 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; }
}