![](/img/trans.png)
[英]How do I place buttons next to each other, sticking them, in Android Layout
[英]Android: How do you define buttons and other widgets outside of a layout?
因此,通常您定義一個XML布局,其中包含單個根節點以及所有包含在其中的按鈕等。 但是,如果您只想在某個位置定義按鈕,以便以后可以引用它們的ID來移動它們,那么在哪里/如何做?
我問這個問題是因為我試圖在FrameLayout中使用“ android:foreground”字段。 因此,我想將其指向我定義的按鈕(或其他小部件)的ID。 但是我在哪里定義此按鈕(或其他小部件)? 如果我只是定義它,通常它不會出現在我不想要的地方嗎?
android:foreground在布局上添加了一個drawable作為疊加層。 您將可繪制對象放入/ res / drawable
至於按鈕,您始終可以在Java中對其進行編碼
只需在res/layout
文件夾中的my_button.xml
文件中使用任何屬性定義按鈕:
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button"
android:layout_width="40dp"
android:layout_height="50dp"
android:layout_margin="2dp"
android:layout_gravity="center"
...
style="@style/MyStyle" />
然后在需要時對其進行充氣:
getLayoutInflater().inflate(R.layout.my_button, null);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.