设置了两个TextView,怎么都没显示出来,界面上一片空白
MainActivity.java
package com.example.oneexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
public class MainActivity extends FragmentActivity implements OnClickListener {
private LinearLayout ask,me;
private Fragment current;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ask=(LinearLayout)findViewById(R.id.ask);
me=(LinearLayout)findViewById(R.id.me);
ask.setOnClickListener(this);
me.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ask:
LeftFragment fragment=new LeftFragment();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
if (current==fragment)
break;
if(!fragment.isAdded())
{
transaction.hide(current);
transaction.add(R.id.cont, fragment);
transaction.commit();
}
else
{
transaction.hide(current);
transaction.show(fragment);
transaction.commit();
}
current=fragment;
break;
case R.id.me:
RightFragment fragments=new RightFragment();
FragmentManager fragmentManagers=getSupportFragmentManager();
FragmentTransaction transactions=fragmentManagers.beginTransaction();
if (current==fragments)
break;
if(!fragments.isAdded())
{
transactions.hide(current);
transactions.add(R.id.cont, fragments);
transactions.commit();
}
else
{
transactions.hide(current);
transactions.show(fragments);
transactions.commit();
}
current=fragments;
break;
default:break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
大概看了看,应该是因为你的Fragment把你的布局盖住了吧
LeftFragment.java
package com.example.oneexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LeftFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.left_activity,container,false);
return view;
}
}
RightFragment.java
package com.example.oneexample;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState)
{
View view=inflater.inflate(R.layout.right_activity,container,false);
return view;
}
}
把布局文件的最外层LinearLayout换为RelativeLayout,然后framelayout布局设为above在两个ask和me同一个外层LinearLayout的上方,然后把这个同一个外层LinearLayout设置为在
android:layout_alignParentBottom="true"
是current没有初始化,应该new Fragment()
第一个LinearLayout就是match_parent ,下面的布局都显示不了
被第一个FrameLayout挤到屏幕外去了当然看不到.
match_parent的原因吧,以后线布局要注意父布局的宽高设置,会影响子布局的