您正在使用TextView
而未将test
布局设置为内容视图。尝试先设置内容视图setContentView(R.layout.test)
然后根据您的需要使用视图。
试试这个:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.YOUR_BUTTON);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
letsee();
}
});
}
public void letsee() {
setContentView(R.layout.test);
TextView tv = (TextView) findViewById(R.id.testview);
tv.setText("DOES THIS EVEN WORK?");
}
}
更新:
您可以使用Fragment
改变你的看法。下面是一个完整的例子:
- 在我们的MainActivity中,有一个名为
Fragment No.1
& Fragment No.2
两个按钮和一个初始Fragment
名为Fragmentone
- 如果按钮
Fragment No.2
按下,FragmentOne
将由FragmentTwo
(其中包含TextView
值代替This is fragment No.2
)
- 如果按下按钮
Fragment No.1
,则FragmentTwo
将被替换为FragmentOne
(其包含TextView
值This is fragment No.1
)
activity_main.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" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Fragment No.1"
android:onClick="selectFrag" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="selectFrag"
android:text="Fragment No.2" />
<fragment
android:name="com.android.fragmentstest.FragmentOne"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fragment_one.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"
android:background="#00ffff">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="This is fragment No.1"
android:textStyle="bold" />
</LinearLayout>
fragment_two.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"
android:background="#ffff00">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is fragment No.2"
android:textStyle="bold" />
</LinearLayout>
FragmentOne。java的
package com.android.fragmentstest;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
FragmentTwo.java
package com.android.fragmentstest;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_two, container, false);
}
}
MainActivity.java
package com.android.fragmentstest;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectFrag(View view) {
Fragment fr;
if(view == findViewById(R.id.button2)) {
fr = new FragmentTwo();
}else {
fr = new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
你也可以看到这个tutorial
希望这会有所帮助〜
来源
2017-04-03 19:53:50
FAT
您可以使用片段吗,将片段绑定到活动并将视图附加到该片段?还是必须来自活动? –
你需要先拨打setContentView –
@paul_hundal抱歉,我不太确定一个片段是什么 – SoulBlack