![](/img/trans.png)
[英]layout_centerInParent overrides layout_alignParentRight/layout_alignParentLeft in Android 4.1.2
[英]Are layout_alignParentRight or layout_alignParentLeft work for include layout?
我有一點問題。
我正在創建一個自定義按鈕小部件
<?xml version="1.0" encoding="utf-8"?>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/barBtn"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:textColor="@color/white"
android:background="@drawable/sel_btn_bar"
android:layout_centerVertical="true"
/>
然后將其放入RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/bar"/>
<include layout="@layout/segm_btn_stores"/>
<include
layout="@layout/btn_bar"
android:layout_alignParentRight="true"/>
</RelativeLayout>
但是直到我將include
標記放入RelativeLayout
,align才能起作用,我可以根據需要移動它。 但是這種方法帶來了另一個問題:按鈕比在RelativeLayout
外部時要窄。 我能做什么? 我想在一處設置按鈕參數。
是的,我可以添加以下行: android:layout_alignParentRight="true"
到Button
標記,它將起作用!
所以問題是:為什么它對Button
標記有效而對include
標記無效?
巴頓和片段布局是有 。
我認為android:layout_centerVertical
屬性(對於您的Button
布局)僅在RelativeLayout
有效(至少在RelativeLayout.LayoutParams
文檔中進行了記錄: http : //developer.android.com/reference/android/widget/RelativeLayout.LayoutParams .html ),因此我不希望它可以在LinearLayout
。
就當按鈕在RelativeLayout
變窄而不是在外部變窄的時候,我真的不知道該說些什么。 給定您提供的XML代碼段,您的RelativeLayout
似乎是文檔的根目錄,即,將<include ... />
標記移到它的外部會生成非法的XML,因此會編譯錯誤(如果使用Eclipse)。
我實際上更喜歡以自定義樣式定義按鈕主題,然后可以在應用程序全局樣式xml中進行設置,例如:
<resources>
<style name="my_custom_style" parent="android:style/Theme.NoTitleBar">
<item name="android:buttonStyle">@style/my_button_style</item>
</style>
<style name="my_button_style" parent="@android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:background">@drawable/my_button_background</item>
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:textColor">@color/my_custom_red_color</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">11sp</item>
</style>
</resources>
然后您可以在AndroidManifest.xml
設置自定義樣式,如下所示:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/application_title"
android:theme="@style/my_custom_style">
<!-- Your Activities, Services etc goes here -->
</application>
我對您的問題感到困惑。 您是否正在嘗試使您的觀點脫離相對布局? 如果要進行布局,則視圖將必須屬於ViewGroup。 對齊不起作用,因為您的視圖位於定義該屬性“ RelativeLayout”的ViewGroup之外
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.