TextView对齐以编程方式添加TextView

时间:2016-03-16 20:06:32

标签: android android-layout

我正在以编程方式添加文本视图,并且由于某种原因,他们将所有加载到顶部 f而不是一个接一个地加载。

虽然问题非常适合LinearLayout,但我想在RelativeLayout中这样做。

以下是相关的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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.android.sunshine.app.DetailActivity.DetailFragment"
    android:id="@+id/detail_top_layout">

    <TextView android:layout_width="wrap_content"
        android:id="@+id/detail_text"
        android:layout_height="wrap_content" />

</RelativeLayout>

和(希望)相关的片段代码:

    private TextView __textViewFactory(String s, RelativeLayout r, Context c, TextView above){
        TextView tv = new TextView(c);
        RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        tv.setTextColor(getResources().getColor(R.color.abc_primary_text_material_light));
        p.addRule(RelativeLayout.ALIGN_BOTTOM, above.getId());

        tv.setText(s);
        r.addView(tv, p);

        return tv;
    }

    @Override
    public void onLoadFinished(Loader<Cursor> cursorLoader, Cursor cursor) {
        Log.d(LOG_TAG, "started");
        RelativeLayout rootView = (RelativeLayout) findViewById(R.id.detail_top_layout);

        Context context = getApplicationContext();
        try {
            cursor.moveToFirst();

            TextView tv = __textViewFactory(Integer.toString(cursor.getInt(COL_WEATHER_ID)), rootView, context, (TextView) findViewById(R.id.detail_text));
            tv = __textViewFactory(Integer.toString(cursor.getInt(COL_WEATHER_DATE)), rootView, context, tv);
            tv = __textViewFactory(cursor.getString(COL_WEATHER_DESC), rootView, context, tv);
        }catch (Exception e){
            Log.e(LOG_TAG, e.getMessage());
        } finally {
            cursor.close();
        }

        ((TextView) rootView.findViewById(R.id.detail_text))
              .setText(mForecastStr);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> cursorLoader) {
        RelativeLayout rootView = (RelativeLayout) findViewById(R.id.detail_top_layout);

        if (rootView != null) {
            int c = rootView.getChildCount();
            TextView tv;
            for (int i = 1; i < c; i++) { // 1 because the first textview _should_ be the one from the layout
                tv = (TextView) rootView.getChildAt(i);
                if(tv.getId() != R.id.detail_text) {
                    rootView.removeViewAt(i);
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:0)

而不是:

    p.addRule(RelativeLayout.ALIGN_BOTTOM, above.getId());

我想你想要这个:

    p.addRule(RelativeLayout.BELOW, above.getId());

答案 1 :(得分:0)

事实证明我没有在我正在创建的视图上设置id,并且在这种情况下android并没有自动执行。我需要补充一下:

private TextView __textViewFactory(String s, RelativeLayout r, Context c, TextView above){
    TextView tv = new TextView(c);
    tv.setId(__getUniqueViewId(c));
    ...
}
...

private Integer __getUniqueViewId(Context c){
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
        return Utility.generateViewId(c);
    } else {
        return View.generateViewId();
    }
}

并在Utility类

static int id = 1;

// Returns a valid id that isn't in use
public static int generateViewId(Context c){
    View rootView = ((Activity)c).getWindow().getDecorView().findViewById(android.R.id.content);
    View v = rootView.findViewById(id);
    while (v != null){
        v = rootView.findViewById(++id);
    }
    return id++;
}