簡體   English   中英

如何在Java中將變量值從Java轉換為xml文件?

[英]How to get variable values from java to xml file in android?

我從.java文件的另一個活動中獲得了一些價值。 現在,我想在我的.xml文件中使用該值。 我應該如何處理。 我應該生成動態xml嗎? 我可以在xml運行時設置一些值嗎?

我的.java文件是:

package com.example.shiza.chemistrylabapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;


public class ExperimentExplaination extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_experiment_explaination);
        Intent i = getIntent();

        int position = i.getIntExtra("Position",0);
    }
}

現在,我要對XML文件使用位置。

.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.shiza.chemistrylabapp.ExperimentExplaination">
</RelativeLayout>

向您的相對布局資源添加一個ID:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"     
    android:id="@+id/layout
    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.shiza.chemistrylabapp.ExperimentExplaination">
</RelativeLayout>

然后,在您的課程中,您可以獲取該引用並設置值。

RelativeLayout rl = (RelativeLayout)findViewById(R.id.layout);

然后設置所需的相對布局的任何屬性

//Example
int width = 90;
rl.setWidth = width;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM