我想讓按鈕將會改變上下文的一部分,但match_parent不工作的屬性。match_parent不工作xml android
了Java的主要文件:
class public MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
Button bn1 = (Button) findViewById(R.id.navbar_1);
Button bn2 = (Button) findViewById(R.id.navbar_2);
bn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentMeters fr = new FragmentMeters();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.layout_to_replace, fr);
ft.commit();
}
});
bn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
FragmentValues fr = new FragmentValues();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.layout_to_replace, fr);
ft.commit();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
然後另一個Java類FragmentMeters(以及類似FragmentValues):
public class FragmentMeters extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_meters, null);
return view;
}
}
及主要XML,像這樣:
<LinearLayout
android:id="@+id/navbar"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="82dp"
android:background="@color/highlighted_text_material_dark"
android:weightSum="4">
<Button
android:id="@+id/navbar_1"
android:text="@string/navbar_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="13sp"
android:background="@android:color/transparent"
android:gravity="bottom|center_horizontal" />
<Button
android:id="@+id/navbar_2"
android:text="@string/navbar_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:textSize="13sp"
android:background="@android:color/transparent"
android:gravity="bottom|center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout_to_replace"
android:orientation="horizontal"
android:layout_below="@+id/navbar"
android:layout_width="match_parent"
android:background="@color/dim_foreground_disabled_material_dark"
android:layout_height="82dp"
android:weightSum="4"/>
而且我試圖用這個fragment_meters.xml替換LinearLayout(id =「layout_to_replace」):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_below="@+id/navbar"
android:layout_width="match_parent"
android:layout_height="82dp"
android:background="@color/dim_foreground_disabled_material_dark"
android:weightSum="4">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="METERS"/>
</LinearLayout>
但我發現了這一點:
匹配它的父的寬度/高度代替。
是的,這工作對我很好。謝謝 !但我不得不使用RadioGroup.LayoutParams而不是LayoutParams –
但是我怎樣才能將它設置爲比match_parent或match_content更多? :/ –
你談論身高? –