我想在同一位置添加两个线性布局,其宽度与父级相同。当我按下Button1时,应该出现linearLayout1,并且应该出现Button2 LinearLayout2。如何在android中执行此操作?
答案 0 :(得分:1)
试试这个: 首先创建your_xml_file.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/firstLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="@+id/secondLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:focusable="false"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:focusable="false"
android:background="@drawable/ic_launcher"
android:layout_below="@id/button1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
和java类......
public class YourActivity extends Activity {
private Button button1,button2;
private LinearLayout linearLayout1,linearLayout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXml);
button1 = (Button).findViewById(android.R.id.button1);
button2 = (Button).findViewById(android.R.id.button2);
linearLayout1 = (LinearLayout).findViewById(android.R.id.firstLinear);
linearLayout2 = (LinearLayout).findViewById(android.R.id.secondLinear);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout2.setVisibility(View.VISIBLE);
linearLayout1.setVisibility(View.GONE);
}
});
}
希望这会对你有所帮助。快乐编码:)
答案 1 :(得分:0)
您可以在代码中使用setVisibility()方法,如:
view.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
答案 2 :(得分:0)
如果您没有使用Fragment
执行任务,则只需使用setVisibility(View.VISIBLE)
和setVisibility(View.INVISIBLE)
/ setVisibility(View.GONE)
(如果其他UI元素不依赖于查看您'将设置GONE)
在onClick
个按钮上。它看起来像这样:
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
答案 3 :(得分:0)
您可以在单个活动中组合多个片段并重复使用 片段也在多个活动中。您可以将片段视为一个片段 活动的模块化部分,具有自己的生命周期,接收 它自己的输入事件,你可以添加或删除 活动正在运行。
以下是如何使用Fragment完成的:
首先创建一个包含2个按钮和一个Framelayout的布局文件,稍后我们将编写代码,将framelayout替换为片段中的内容。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/ll1"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
android:id="@+id/bt1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:id="@+id/bt2"
android:layout_alignBottom="@+id/frame"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll1"
android:id="@+id/frame"/>
</RelativeLayout>
接下来创建2个片段,例如FragmentA和fragmentB
public class fragmentA extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
创建第二个片段B
public class fragmentB extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
现在添加带有2个按钮的Activity代码,当单击按钮1调用fragmentManager.beginTransaction().replace(R.id.frame, new fragmentA()).commit();
时,这将使用fragmentA的内容替换framelayout,类似于单击按钮2时调用fragmentManager.beginTransaction().replace(R.id.frame, new fragmentB()).commit();
< / p>
完整代码:
public class MyFragment extends ActionBarActivity implements View.OnClickListener {
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tt);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (v.getId()) {
case R.id.bt1:
fragmentManager.beginTransaction()
.replace(R.id.frame, new BlockCallers()).commit();
break;
case R.id.bt2:
fragmentManager.beginTransaction()
.replace(R.id.frame, new smsSetting()).commit();
}
}
}