Android:Listview单元格不显示所选的休闲字体

时间:2017-01-17 20:19:01

标签: java android listview fonts

我尝试将 listview单元格的文本框的 font 设置为casual(在Android Studio中定义并可选择) ,通过使用Android Studio的设计师。我有以下代码: activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#FFFFFF"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:layout_scrollFlags="scroll|enterAlways">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lst"
    android:footerDividersEnabled="false"
    android:headerDividersEnabled="false" />

</android.support.design.widget.CoordinatorLayout>

然后是cell的代码:listcell.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:p1="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
p1:minWidth="25px"
p1:minHeight="25px"
p1:layout_width="match_parent"
p1:layout_height="wrap_content"
p1:id="@+id/relativeLayout1"
p1:background="#ffffffff">
<LinearLayout
    p1:orientation="vertical"
    p1:minWidth="25px"
    p1:minHeight="25px"
    p1:layout_width="wrap_content"
    p1:layout_height="wrap_content"
    p1:id="@+id/linearLayout1"
    p1:layout_centerVertical="true">
    <TextView
        p1:text="Mittelmäßig"
        p1:layout_width="385dp"
        p1:layout_height="60dp"
        p1:id="@+id/txtTitle"
        p1:textSize="18sp"
        p1:textColor="@color/colorWhite"
        p1:background="#4b6df2"
        p1:paddingLeft="60dp"
        p1:gravity="center_vertical"
        tools:fontFamily="casual" />
</LinearLayout>
</RelativeLayout>

然后我定义了我的适配器:StimmungsAdapter.java:

public class StimmungsAdapter extends BaseAdapter {
private Activity _context;
private ArrayList<String > stimmungen;
private int ImageId;

public StimmungsAdapter(Activity context)
{
    stimmungen = new ArrayList<String>();
    stimmungen.add("Überhaupt nicht");
    stimmungen.add("Wenig");
    stimmungen.add("Mittelmäßig");
    stimmungen.add("Ziemlich");
    stimmungen.add("Extrem");

    _context = context;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    String nt = stimmungen.get(position);
    if (view == null)
        view = _context.getLayoutInflater().inflate(R.layout.listcell, null);

    TextView txtTitle =(TextView) view.findViewById(R.id.txtTitle);
    txtTitle.setText(nt);

    return view;
    }

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

并在我的MainActivity.java的onCreate函数中调用它:

...
    StimmungsAdapter adapter = new StimmungsAdapter(this);
    ListView lst = (ListView)findViewById( R.id.lst);
    lst.setAdapter(adapter);
...

我尝试了很多东西,但是单元格显示为默认 字体。当我创建普通TextView(不在列表视图中作为单元格)时,会显示casual字体。 为什么字体不显示在listview单元格中?

1 个答案:

答案 0 :(得分:1)

RewriteRule ^sitemap\.xml$ xmlsitemap.php [L] RewriteRule ^sitemap$ sitemap.php [L] 替换为tools:fontFamily(或在您的情况下,android:fontFamily)`

问题是您的p1:fontFamily属性前缀为fontFamily。这使得属性值仅适用于布局编辑器预览,并在编译为apk时被丢弃。