自定义AppCompatButton drawableLeft?

时间:2015-12-01 07:40:06

标签: android button android-appcompat compound-drawables

如何在下图中放置public class ListViewAdapter extends BaseAdapter { // Declare Variables Context mContext; LayoutInflater inflater; private List<WorldPopulation> worldpopulationlist = null; private ArrayList<WorldPopulation> arraylist; public ListViewAdapter(Context context, List<WorldPopulation> worldpopulationlist) { mContext = context; this.worldpopulationlist = worldpopulationlist; inflater = LayoutInflater.from(mContext); this.arraylist = new ArrayList<WorldPopulation>(); this.arraylist.addAll(worldpopulationlist); } public class ViewHolder { TextView rank; TextView country; TextView population; ImageView flag; } @Override public int getCount() { return worldpopulationlist.size(); } @Override public WorldPopulation getItem(int position) { return worldpopulationlist.get(position); } @Override public long getItemId(int position) { return position; } public View getView(final int position, View view, final ViewGroup parent) { final ViewHolder holder; if (view == null) { holder = new ViewHolder(); view = inflater.inflate(R.layout.listview_item, null); // Locate the TextViews in listview_item.xml holder.rank = (TextView) view.findViewById(R.id.rank); holder.country = (TextView) view.findViewById(R.id.country); holder.population = (TextView) view.findViewById(R.id.population); // Locate the ImageView in listview_item.xml holder.flag = (ImageView) view.findViewById(R.id.flag); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } // Set the results into TextViews holder.rank.setText(worldpopulationlist.get(position).getRank()); holder.country.setText(worldpopulationlist.get(position) .getCountry()); holder.population.setText(worldpopulationlist.get(position) .getPopulation()); // Set the results into ImageView holder.flag.setImageResource(worldpopulationlist.get(position) .getFlag()); // Listen for ListView Item Click view.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent summaryIntent = new Intent(mContext, SecondActivity.class); Bundle b = new Bundle(); b.putInt("Integer", position); summaryIntent.putExtras(b); mContext.startActivity(summaryIntent); Toast.makeText(mContext, "Position:" + position, Toast.LENGTH_SHORT).show(); Intent intent = new Intent(mContext, SecondActivity.class); // Send single item click data to SingleItemView Class // depending on "position", for example // Pass all data rank // intent.putExtra("rank", // (worldpopulationlist.get(position).getRank())); // ...likewise pass all other data ... mContext.startActivity(intent); } }); return view; } // Filter Class public void filter(String charText) { charText = charText.toLowerCase(Locale.getDefault()); worldpopulationlist.clear(); if (charText.length() == 0) { worldpopulationlist.addAll(arraylist); } else { for (WorldPopulation wp : arraylist) { if (wp.getCountry().toLowerCase(Locale.getDefault()) .contains(charText) ) { worldpopulationlist.add(wp); } if(wp.getRank().toLowerCase(Locale.getDefault()) .contains(charText)) { worldpopulationlist.add(wp); } if (wp.getPopulation().toLowerCase(Locale.getDefault()) .contains(charText)) { worldpopulationlist.add(wp); } } } notifyDataSetChanged(); } } drawableLeft的按钮文字?

AppCompatButton

我尝试应用AppCompatButton,但文字正在从可绘制的文件向右移动。

paddingLeft

到目前为止结果看起来像

enter image description here

布局

<android.support.v7.widget.AppCompatButton
                android:id="@+id/filterBy"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:drawableLeft="@drawable/ic_btn_filter"
                android:paddingLeft="20dp"
                android:text="@string/filter_by"
                android:textAllCaps="false" />

解释要求明确的要求。

  

使用 <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/profileHeader" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="10dp"> <android.support.v7.widget.AppCompatButton android:id="@+id/findFriends" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft="@drawable/ic_btn_search" android:text="@string/find_friends" android:textAllCaps="false" /> <android.support.v7.widget.AppCompatButton android:id="@+id/filterBy" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:drawableLeft="@drawable/ic_btn_filter" android:text="@string/filter_by" android:textAllCaps="false" /> </LinearLayout> 属性调整两个按钮的宽度和我要做的是,文本应该在按钮的中心   并且drawable应该在文本左侧对齐。

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

使用

 android:drawablePadding=""

答案 1 :(得分:0)

可能你不能,如果你想这样,那么你必须使用像enter link description here

这样的复杂布局。