1

你能从 Android 风格中获得 LayoutPrams 吗?如果是这样,我该怎么做?- API 8 级

(我正在尝试将样式应用于动态创建的元素 - 如果有更好的方法可以做到这一点,我会全力以赴)


我已经尝试过这种LayoutInflater方式(您制作单个 .xml 文件,然后将它们作为模板调用并转换它们 - .xml 元素android:style="..."在其中 - 这似乎是很多不必要的工作;最终对我不起作用.

示例代码(使用 LayoutInflater)[不工作/样式]

public void build_gui() {

    // can load 'template' layout elements
    LayoutInflater layout_Inflater = getLayoutInflater();

    // the master_layout - this is the bottom layer
    LinearLayout m_ll = (LinearLayout) layout_Inflater.inflate(
            R.layout.main_master_linearlayout, null);
    ScrollView sv = new ScrollView(this);
    LinearLayout ll = (LinearLayout) layout_Inflater.inflate(
            R.layout.main_master_linearlayout, null);

    for (Feed_element fe : mApp.feed_elements) {
        Log.d(tag, "GUI: " + fe.title);
        RelativeLayout rl = (RelativeLayout) layout_Inflater.inflate(
                R.layout.main_element_container_relativelayout, null);

        // init - preview image
        ImageView iv_preview = (ImageView) layout_Inflater.inflate(
                R.layout.main_element_preview_imageview, null);
        // init - title
        TextView tv_title = (TextView) layout_Inflater.inflate(
                R.layout.main_element_title_textview, null);
        // init - desc
        TextView tv_desc = (TextView) layout_Inflater.inflate(
                R.layout.main_element_desc_textview, null);

        // set the preview bitmap to teh default of no preview
        Bitmap preview = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.no_preview);

        if (fe.preview != "") {
            Log.d(tag, "Downloading IMG: " + fe.preview);
            // if there was a preview get the image
            preview = mApp.web.downloadFile(fe.preview);
        }

        // get the pxl aprox
        int pxl = (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, (float) PREVIEW_SIZE, this
                        .getResources().getDisplayMetrics());
        iv_preview.setImageBitmap(Bitmap.createScaledBitmap(preview, pxl,
                pxl, true));

        if (fe.title != "" || fe.title != null) {
            tv_title.setText(fe.title);
        }

        if (fe.description != "" || fe.description != null) {
            tv_title.setText(fe.description);
        }

        // THIS is the type of hard coding my styles that i would like to avoid
        RelativeLayout.LayoutParams base_layout_pram = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        RelativeLayout.LayoutParams iv_preview_prams = base_layout_pram;
        iv_preview_prams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        iv_preview_prams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

        RelativeLayout.LayoutParams tv_title_prams = base_layout_pram;
        tv_title_prams.addRule(RelativeLayout.RIGHT_OF, iv_preview.getId());

        RelativeLayout.LayoutParams tv_desc_prams = base_layout_pram;
        tv_desc_prams.addRule(RelativeLayout.RIGHT_OF, iv_preview.getId());
        tv_desc_prams.addRule(RelativeLayout.BELOW, tv_title.getId());

        rl.addView(iv_preview);
        rl.addView(tv_title, tv_title_prams);
        rl.addView(tv_desc, tv_desc_prams);

        ll.addView(rl);
        mApp.feed_id_count++;
    }

    sv.addView(ll);
    m_ll.addView(sv);
    setContentView(m_ll);
}

在上面的代码中,我知道我必须设置所有的RelativeLayout.RIGHT_OF's 等等......但我试图避免以下情况:

    // THIS is the type of hard coding my styles that i would like to avoid
    RelativeLayout.LayoutParams base_layout_pram = new LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

示例模板

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    style="@style/main_element_preview_imageView">

</ImageView>

样式示例

<style name="main_element_preview_imageView">
    <item name="android:layout_width">75dp</item>
    <item name="android:layout_height">75dp</item>
    <item name="android:layout_alignParentLeft">true</item>
    <item name="android:layout_centerVertical">true</item>
    <item name="android:layout_marginRight">5dp</item>
    <item name="android:scaleType">fitStart</item>
    <item name="android:adjustViewBounds">true</item>
</style>

想法

我相信可以创建我自己的重载类,并将这些属性设置为默认值——但同样,这似乎与拥有元素样式的想法适得其反。

4

1 回答 1

1

我相信在你的风格中你必须声明风格的父母,即

> <style name="main_element_preview_imageView" parent="Widget.ImageView">
于 2011-12-30T20:07:12.680 回答