相关疑难解决方法(0)

如何在相对度量中指定Android GridLayout单元格的宽度和高度?

我试图弄清楚GridLayout是如何工作的,但是我从文档中无法弄清楚的一件事是如何或者如果可以控制网格单元的大小.

假设我想要一个两个两个网格,其中每个单元格占据屏幕空间的25%(半高,半宽) - 我可以这样做吗?

使用LinearLayout,我可以通过在一个垂直中嵌套两个水平LinearLayout然后为所有元素指定权重 1来实现此目的.GridLayout虽然不支持weight属性.

android android-layout

40
推荐指数
3
解决办法
7万
查看次数

Android相对布局,按钮宽度使用重量

我已经使用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)

layout android button width

13
推荐指数
4
解决办法
8万
查看次数

Android布局重量不适用于相对布局

在此输入图像描述

嘿伙计我自己第一次尝试实现布局重量,有点我尝试使用线性布局它运行良好,但是当我尝试使用相对和线性布局时出现问题.你们可以告诉我这里有什么问题吗

我的XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/Linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="100" >

<RelativeLayout
    android:id="@+id/Rlayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="45" >

    <Gallery
        android:id="@+id/Gallery01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:spacing="4dip" >
    </Gallery>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/Rlayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="10"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/ImageView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />

    <ImageView
        android:id="@+id/ImageView02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="fitXY" />
</RelativeLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navbar"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="45"
    android:background="@drawable/button1"
    android:orientation="horizontal"
    android:weightSum="100" >

    <Button
        android:id="@+id/makerback"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerback" />

    <Button
        android:id="@+id/makerphoto"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerphoto" />

    <Button
        android:id="@+id/makerselves"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="20"
        android:background="@drawable/makerselves" …
Run Code Online (Sandbox Code Playgroud)

android android-emulator android-intent android-layout

7
推荐指数
2
解决办法
2万
查看次数

将LinearLayout和"百分比"边距居中

编辑:我已收到两个关于固定保证金的综合答案.虽然我已经决定使用固定边距而不是重量边距,但最初的问题仍未解决.

我想在Android中获得以下设计:

期望的布局

一个居中的垂直列表(TextViews,EditViews等),它留下大约10%的水平空间作为左/右边距,带有背景.

我尝试过但没有工作/部分工作的内容:

  • LinearLayout,vertical,作为顶级布局.如果重力设置为"居中",则背景将限制为布局的大小.另外,如何以这种方式设置百分比边距(宽度)?
  • RelativeLayout上的LinearLayout:背景有效,水平居中工作,权重不存在.
  • LinearLayout上的LinearLayout:背景工作,权重工作,水平居中将所有可用空间推向右侧.

(在最后两种情况下,我的Eclipse也抱怨其中一个布局是多余的.)

我没有发布代码,认为这更像是一个与原则相关的问题.实现这一目标的最佳方法是什么?

谢谢.

对应于最后一个测试用例的XML:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:weightSum="1.0"
    android:background="#013c57" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:layout_gravity="center"
        android:orientation="vertical" >

        <!-- Stuff -->
    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android margin centering android-layout android-percentrelativelayout

6
推荐指数
1
解决办法
9617
查看次数