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
などを設定する必要があることを理解していますが、次のことを避けようとしています:
// 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>
考え
これらのプロパティをデフォルトに設定して、独自のオーバーロードされたクラスを作成することは可能だと思いますが、繰り返しますが、これは要素スタイルを持つという考えには逆効果のようです。