我想創建一個自定義底部的按鈕欄佈局的工作,我創建了一個XML文件:重量不以定製的Android組件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="@+id/media_menu_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:text="@string/media_menu_button" />
<Button
android:id="@+id/scenario_menu_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:text="@string/scenario_menu_button" />
<Button
android:id="@+id/rooms_menu_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:text="@string/rooms_menu_button" />
<Button
android:id="@+id/shortcut_menu_button"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_weight="1"
android:text="@string/shortcut_menu_button" />
,你可以看到我已經給出的所有的按鈕寬度0dp和重量的1然後,我已經創建了延伸的線性佈局類的類:
public class BeLightBottomBar extends LinearLayout implements OnClickListener {
private LayoutInflater mInflater;
private Context contexnt;
private Button mShortcutMenuButton;
private Button mRoomsMenuButton;
private Button mScenarioMenuButton;
private Button mMediaMenuButton;
public BeLightBottomBar(Context context, AttributeSet attrs) {
super(context, attrs);
//inflate the view
this.contexnt = context;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout barView = (LinearLayout) mInflater.inflate(R.layout.belight_bottombar, null);
addView(barView);
//get all the instances of the components of the bar
mShortcutMenuButton = (Button) barView.findViewById(R.id.shortcut_menu_button);
mRoomsMenuButton = (Button) barView.findViewById(R.id.rooms_menu_button);
mScenarioMenuButton = (Button) barView.findViewById(R.id.scenario_menu_button);
mMediaMenuButton = (Button) barView.findViewById(R.id.media_menu_button);
//set this as a click listener
mShortcutMenuButton.setOnClickListener(this);
mRoomsMenuButton.setOnClickListener(this);
mScenarioMenuButton.setOnClickListener(this);
mMediaMenuButton.setOnClickListener(this);
...
...
...
}
的問題是當我這個類添加到主活性XML
<belight.homecontrol.components.BeLightBottomBar
android:id="@+id/button_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_margin="0dp"
android:padding="0dp" />
重量停止工作,所有按鈕都不同。我不知道爲什麼!? 如果我只是將底部的bar xml代碼粘貼到主xml文件中,它可以正常工作,但只有在將它作爲一個整體使用時纔會出現此問題。
P.S. 以這種方式創建組件是否是一種很好的做法?或者,也許我做錯了什麼?
感謝, 丹
謝謝!!!你的解決方案工作,認爲,這將是很高興知道幕後發生了什麼,使其不能像我做的那樣工作(只是遵循互聯網上找到的一個例子)。也許當你使用addView方法添加一個視圖時,它不會計算寬度值等等。無論如何感謝 – RCB
與該版本的addView它沒有。 addView有幾個選項,包括一個需要寬度和高度參數的選項。如果沒有這些,則使用「此ViewGroup的默認參數」。如果你只是在你的膨脹線上使用'this'代替父代替'null',並且完全丟棄了addView,它可能也會起作用。無論哪種方式,它開心適合你。 – Geobits