gpt4 book ai didi

android - 你能从 Android Style 中获取 LayoutPrams 吗?

转载 作者:行者123 更新时间:2023-11-30 04:24:50 27 4
gpt4 key购买 nike

你能从 Android Style 中获取 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 等等...但我试图避免以下情况:

    // 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>

想法

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

最佳答案

我相信在您的样式中您必须声明样式的父级,即

> <style name="main_element_preview_imageView" parent="Widget.ImageView">

关于android - 你能从 Android Style 中获取 LayoutPrams 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683461/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com