如何将包含网站URL的字符串数组添加到ScrollView?

时间:2014-12-20 15:45:28

标签: android android-layout

我已经在我的Android应用程序中添加了一个字符串数组,用于获得它的人员的学分。 每个项目都具有<item>role,name,website url</item>的结构 我想在滚动视图中发布每个项目,如下所示:

role1 
name
url

role2
name
url

但我不知道该怎么做。 我可以直接在布局XML文件中执行此操作吗? 如果没有,我怎么能以不同的方式做到这一点?

2 个答案:

答案 0 :(得分:1)

如果您需要纯XML解决方案,请使用<string>新行代替<string-array>

的strings.xml:

<string name="multiline_text">
Philip J. Fry
\nDelivery Boy
\nhttp://en.wikipedia.org/wiki/Fry_%28Futurama%29
\n
\nTuranga Leela
\nCaptain
\nhttp://en.wikipedia.org/wiki/Leela_%28Futurama%29
</string>

activity_main.xml中:

<TextView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:text="@string/multiline_text"
  android:autoLink="web"/>

android:autoLink="web"可以点击链接。

答案 1 :(得分:0)

您可以在strings.xml文件中使用包含所有数据的HTML字符串,并使用ScrollView中的TextView显示它:

myTextView.setText(Html.fromHtml(getResources().getString(R.string.my_string));

或者,您可以将ListView与包含数据类型元素的自定义ArrayAdapter一起使用。这是一个例子:

Employee.java:

public class Employee {
    String role, name, url;
}

EmployeeAdapter.java:

public class EmployeeAdapter extends ArrayAdapter<Employee> {
    private int mResource;

    public EmployeeAdapter(FindPenguinsActivity activity, int resource) {
        super(activity, resource);
        mResource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)  {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(mResource, null);
        }

        // You should use the ViewHolder pattern here, this is just for simplicity
        TextView role = (TextView)convertView.findViewById(R.id.role);
        TextView name = (TextView)convertView.findViewById(R.id.name);
        TextView url = (TextView)convertView.findViewById(R.id.url);

        Employee employee = getItem(position);
        role.setText(employee.role);
        role.setText(employee.name);
        role.setText(employee.url);

        return convertView;
    }
}

employee.xml:

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

    <TextView
        android:id="@+id/role"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/website"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

现在您可以将适配器添加到ListView:

ListView listView = findViewById(R.id.list); // Use the id of your ListView in the activity's xml layout file here

List<Employee> employeeList = new List<Employee>(3);

// add Employee objects to the list

EmployeeAdapter adapter = new EmployeeAdapter(this, R.layout.employee);
adapter.addAll(employeeList);
listView.setAdapter(adapter);