【问题标题】:Android: How to Change to Text Size in ListView become bigger?Android:如何将 ListView 中的文本大小更改为更大?
【发布时间】:2012-11-30 10:30:28
【问题描述】:

我对以下情况感到困惑,我创建了一个 LinearLayout 来显示我手机中的联系人列表。但是,我不知道在哪里可以将 LinearLayout 中的文本设置为更大!请帮助,谢谢!!!!

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ListView android:layout_width="wrap_content"
              android:id="@+id/contactList"
              android:layout_height="0dp"
              android:padding="10dp"
              android:textSize="200sp"
              android:layout_weight="10"/>
    <CheckBox android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/showInvisible"
              android:text="@string/showInvisible"/>
    <Button android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/addContactButton"
            android:text="@string/addContactButtonLabel"/>
</LinearLayout>

contactmanager.xml如下:

package com.example.android.contactmanager;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public final class ContactManager extends Activity
{

    public static final String TAG = "ContactManager";

    private Button mAddAccountButton;
    private ListView mContactList;
    private boolean mShowInvisible;
    //public BooleanObservable ShowInvisible = new BooleanObservable(false);
    private CheckBox mShowInvisibleControl;

    /**
     * Called when the activity is first created. Responsible for initializing the UI.
     */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_manager);

        ArrayAdapter<String> filesAdapter = new ArrayAdapter<String>(this, 
        R.layout.simplest_list_item_1, topFilesArray);
        filesList.setDivider(null);

        // Obtain handles to UI objects
        mAddAccountButton = (Button) findViewById(R.id.addContactButton);
        mContactList = (ListView) findViewById(R.id.contactList);
        mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);

        // Initialise class properties
        mShowInvisible = false;
        mShowInvisibleControl.setChecked(mShowInvisible);

        // Register handler for UI elements
        mAddAccountButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.d(TAG, "mAddAccountButton clicked");
                launchContactAdder();
            }
        });
        mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.d(TAG, "mShowInvisibleControl changed: " + isChecked);
                mShowInvisible = isChecked;
                populateContactList();
            }
        });

        // Populate the contact list
        populateContactList();
    }

    /**
     * Populate the contact list based on account currently selected in the account spinner.
     */
    private void populateContactList() {
        // Build adapter with contact entries
        Cursor cursor = getContacts();
        String[] fields = new String[] {
                ContactsContract.Data.DISPLAY_NAME
        };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
                fields, new int[] {R.id.contactEntryText});
        mContactList.setAdapter(adapter);
    }

    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] {
                ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME
        };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible ? "0" : "1") + "'";
        //String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + (mShowInvisible.get() ? "0" : "1") + "'";
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return this.managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

    /**
     * Launches the ContactAdder activity to add a new contact to the selected account.
     */
    protected void launchContactAdder() {
        Intent i = new Intent(this, ContactAdder.class);
        startActivity(i);
    }


}

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content">
    <TextView android:text="@+id/contactEntryText"
              android:id="@+id/contactEntryText"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"/>
</LinearLayout>

【问题讨论】:

  • 每个元素使用哪个布局项?
  • 发布您的项目布局 xml 文件。否则,只需在项目布局的 xml 文件中增加 textView 的大小。它会在您的ListView 中提供更大尺寸的文本
  • @Praveen,我将如何以 texView 大小编码? android:textsize=10?
  • +1 是的。就是这样..您使用了多少种布局??
  • 向我们展示您的 R.layout.contact_entry 布局

标签: android eclipse listview android-linearlayout


【解决方案1】:

把Textview的android:textSize="10dip"这个属性设置成contact_entry.xml,就可以解决你的问题了。

【讨论】:

  • 创建后,android:textSize="10" 行如下: error: Error: Integer types not allowed (at 'textSize' with value '10')。
  • 我可以再问你一个愚蠢的问题吗?我是android新手,我确实有一个contactmanager.java,它现在使用contactmanager.xml,那么我要如何添加你的simple_list_item_1.xml?
  • @SeanTan 您问题的 XML 代码来自contactmanager.xml 文件?
  • 是的,它来自contactmanager.xml,我需要在里面插入你的代码吗?
  • @SeanTan 不,不要将此代码放入您的 xml 文件中,制作单独的 xml 文件。
【解决方案2】:

最好在 sp 中设置文本的大小(Sp 相对于设备的正常字体大小独立缩放。)或者您可以使用android:textAppearance="?android:attr/textAppearanceLarge

【讨论】:

    猜你喜欢
    • 2015-02-01
    • 2014-06-04
    • 2011-01-28
    • 1970-01-01
    • 2021-05-12
    • 2015-05-20
    • 2016-01-30
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多