内置动态文本的可重复使用图像

时间:2015-12-29 16:50:42

标签: java android imageview horizontalscrollview

您好我有一个Horizo​​ntalScrollView,我想用一些固定大小的图像填充该图像中的动态文本。以下是其中一张图片的示例:

def convert(input):
    converters = [int, float, json.loads]
    for converter in converters:
        try:
            return converter(input)
        except (TypeError, ValueError):
            pass
    # here we assume if all converters failed, it's just a string
    return input

s = "1|2.3|a|[4,5]"
result = [convert(x) for x in s.split("|")]

我想用可变数量的这些填充我的Horizo​​ntalScrollView,并且能够动态更新它们的首字母并经常得分。现在,我只能在我的xml文件中使用预定义数量的TextView填充Horizo​​ntalScrollView。如何为这些图像创建模板布局并动态更新其中的值?理想情况下,此图像只是一个圆圈,其首字母和分数居中。谢谢!

3 个答案:

答案 0 :(得分:1)

我们有类似的任务。我建议使用以下内容:1。创​​建九个补丁图像并定义要放置文本的内容区域。还为此drawable定义可伸缩的补丁。 2.对于TextView,将此drawable定义为背景并为其提供一些填充。我希望它会有所帮助。如果内容被更改,则图像应根据内容进行拉伸。

答案 1 :(得分:0)

您可以将图像设置为相对布局的背景,并在模板布局中的此相对布局的中心设置文本。

答案 2 :(得分:0)

主要活动

    public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listView1);
        adapter = new MyAdapter(this);
        adapter.addItem(new ModelPlayer("AK", "37"));
        adapter.addItem(new ModelPlayer("BK", "36"));
        adapter.addItem(new ModelPlayer("CK", "35"));
        adapter.addItem(new ModelPlayer("DK", "34"));
        listView.setAdapter(adapter);
    }

}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/btnGetList" >
    </ListView>

</RelativeLayout>

MyAdapter

    public class MyAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<ModelPlayer> players;

    public MyAdapter(Context context) {
        super();
        this.context = context;
        players = new ArrayList<ModelPlayer>();
    }

    public MyAdapter(Context context, ArrayList<ModelPlayer> players) {
        super();
        this.context = context;
        this.players = players;
    }

    public void addItem(ModelPlayer player) {
        players.add(player);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return players.size();
    }

    @Override
    public Object getItem(int position) {
        return players.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private class ViewHolder {
        TextView txtPlayer, txtScore;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.layout_item, null);
            holder = new ViewHolder();
            holder.txtPlayer = (TextView) convertView.findViewById(R.id.txtPlayer);
            holder.txtScore = (TextView) convertView.findViewById(R.id.txtScore);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ModelPlayer player = players.get(position);
        holder.txtPlayer.setText(player.getPlayer());
        holder.txtScore.setText(player.getScore());
        return convertView;
    }
}

layout_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/rlBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_circle"
        android:padding="20dp" >

        <TextView
            android:id="@+id/txtPlayer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="AK" />

        <TextView
            android:id="@+id/txtScore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/txtPlayer"
            android:layout_centerHorizontal="true"
            android:text="34" />
    </RelativeLayout>

</LinearLayout>

ModelPlayer

 public class ModelPlayer {
    private String player, score;

    public ModelPlayer(String player, String score) {
        super();
        this.player = player;
        this.score = score;
    }

    public String getPlayer() {
        return player;
    }

    public void setPlayer(String player) {
        this.player = player;
    }

    public String getScore() {
        return score;
    }

    public void setScore(String score) {
        this.score = score;
    }
}

抽拉/ shape_circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >

    <solid android:color="#fa0000" >
    </solid>

</shape>

我希望这个例子对你有帮助