iCa*_*arp 13 layout android button width
我已经使用layout_weight
参数将按钮的宽度设置为总布局宽度的70%,但似乎我缺少一些重要的细节才能使它工作.
(另一个解决方案是以display.getWidth()
编程方式工作,但它也不起作用,因为我不知道我的.xml应该是什么样子如果我选择设置宽度button.setWidth()
)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1.0">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:id="@+id/userVersionTextViewNew"
android:gravity="center"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:gravity="center"
android:layout_above="@id/userVersionTextViewNew"
android:id="@+id/userSoftSerialNumberTextView"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/logo_200"
android:layout_above="@id/userSoftSerialNumberTextView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:gravity="center"
android:layout_below="@id/userVersionTextViewNew"
android:id="@+id/dummyTextView"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/loginButton"
android:text="???????"
android:layout_centerHorizontal="true"
android:layout_below="@id/dummyTextView"
android:layout_weight="0.7"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/demoLoginButton"
android:text="??????????? ?????"
android:layout_centerHorizontal="true"
android:layout_below="@id/loginButton"
android:layout_weight="0.7"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
hcp*_*cpl 29
问题
您不能在RelativeLayout上使用layout_weight参数.这些是LinearLayout的参数.我将在下面提供有关差异的更多信息.但首先是关于这个问题的解决方案
一个办法
使用LinearLayout,您可以使用权重分布将元素放在一行中.添加layout_weights时不要忘记使用0dp宽度!以下示例显示重量分布为70/30.
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="1.0" >
<Button
android:text="left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70" />
<Button
android:text="right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
所有这些都在你已经在代码中的RelativeLayout中.这个答案的其余部分是背景信息,每个人都应该阅读这些问题以了解他们正在做什么.
的RelativeLayout
每当你从一个包含多个元素的布局开始时,我建议你更喜欢RelativeLayout而不是Linear的东西.RelativeLayout功能非常强大,可让您相对于彼此定位元素(leftOf,below,...).在大多数情况下,这比你需要的更多.
来自android开发文档的一个例子(相信我就在那里):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="@string/done" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
的LinearLayout
LinearLayout看起来也很强大,但为了让所有东西都只用Linears排序,你很可能会开始嵌套这些布局.这就是明智的表现.
再次来自android开发文档的一个例子.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<EditText
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
小智 14
试试这个..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:id="@+id/userVersionTextViewNew"
android:gravity="center"
android:layout_centerVertical="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:gravity="center"
android:layout_above="@id/userVersionTextViewNew"
android:id="@+id/userSoftSerialNumberTextView"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/logo_200"
android:layout_above="@id/userSoftSerialNumberTextView"
android:layout_centerHorizontal="true"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="15px"
android:gravity="center"
android:layout_below="@id/userVersionTextViewNew"
android:id="@+id/dummyTextView"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity = "center_horizontal"
android:layout_below="@id/dummyTextView"
android:id="@+id/loginButtonLayout"
android:weightSum="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/loginButton"
android:text="???????"
android:layout_weight="0.7"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity = "center_horizontal"
android:layout_below="@id/loginButtonLayout"
android:weightSum="1.0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/demoLoginButton"
android:text="??????????? ?????"
android:layout_weight="0.7"/>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
小智 6
我觉得layout_weight
里面没有作品RelativeLayout
.也许你应该添加一个LinearLayout
内部RelativeLayout
并使用layout_weight
内部.
另外,在使用时,layout_weight
您通常必须将对象的宽度或高度定义为0dp
,所以在您的情况下,像这样:
android:layout_weight="0.7"
android:layout_height="0dp"
Run Code Online (Sandbox Code Playgroud)
我知道这个问题已经过时了,但仅针对寻找解决方案的人:
谷歌推出了名为android.support.percent的新API
PercentRelativeLayout
完全是你的情况:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<!-- Other controls -->
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/loginButton"
android:text="???????"
android:layout_centerHorizontal="true"
android:layout_below="@id/dummyTextView"
app:layout_widthPercent="70%"/>
<!-- Other controls -->
</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
75681 次 |
最近记录: |