How to display a TextView from another layout

喜你入骨 提交于 2020-01-02 15:51:33

问题


I have a mainactivity layout and another layout with a textview. I want to set value to textview from my mainactivity file and display it in the mainlayout file. How can I go about this. I'm using layout inflator and am getting the refrence id of Textview successfully.

Main Activity:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View vi = inflater.inflate(R.layout.layout2, null); //log.xml is your file.
    TextView tv = (TextView)vi.findViewById(R.id.text1);

    System.out.println("Textview= "+tv);
    tv.setText("hELLLLOO");
}
}

Main_Layout.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
</RelativeLayout>

Layout2.xml:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
 >
 <TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world"
    android:layout_gravity="center" 
    android:textColor="#000000"/>
 </LinearLayout>

回答1:


You can use intent and pass the value to the next Activity. You only inflate the layout but that is not added to Activity.

You will have to get the reference to RelativeLayout and add the inflated view to it

Have a id for RelativeLAyout

<RelativeLayout android:id="@+id/relativelayout

Then

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
View vi = inflater.inflate(R.layout.layout2, null); //log.xml is your file.
TextView tv = (TextView)vi.findViewById(R.id.text1);
tv.setText("hELLLLOO");
rl.addView(vi);

You may also use the

http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/

Or

In MainActivity

Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("key","hello");
startActivity(intent);

Then in SecondActivity

// Inflate Layout 2 and set text to textview.
String value = getIntent().getStringExtra("key");



回答2:


do something like this in MainActivity.java

View view;
/* We inflate the xml which gives us a view */
view = mInflater.inflate(R.layout.my_list_custom_row, false);

 /* Get the widget with id name which is defined in the xml of the row */
 TextView name = (TextView) view.findViewById(R.id.name);

 /* Populate the row's xml with info from the item */
 name.setText(myObject.getName());

Where mInflater is an Object of LayoutInflator. Or Check this link. hope this helps.



来源:https://stackoverflow.com/questions/22244155/how-to-display-a-textview-from-another-layout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!