标签: android-linearlayout

Android:如何以编程方式设置布局的大小

作为Android应用程序的一部分,我正在构建一个按钮集.这些按钮是嵌套的LinearLayouts集的一部分.使用权重我根据包含父LinearLayout的大小自动调整自身大小.该想法是,基于屏幕的像素数和密度,将包含布局的大小设置为多个像素; 并根据该更改让按钮设置自行调整大小.

那么问题是:如何调整布局大小.

我已经尝试了几种建议的技术,没有一种能够接近工作.以下是构建按钮集的XML的子集:

    <LinearLayout android:layout_height="104pt" android:id="@+id/numberPadLayout" android:orientation="horizontal" android:layout_width="104pt"
    android:background="#a0a0a0"
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true"
>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="1" android:id="@+id/button1" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="4" android:id="@+id/button4" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="7" android:id="@+id/button7" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="-" android:id="@+id/buttonDash" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>
    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout2" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="2" android:id="@+id/button2" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="5" android:id="@+id/button5" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="8" android:id="@+id/button8" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
        <Button android:text="0" android:id="@+id/button0" android:layout_weight="2" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
    </LinearLayout>

    <LinearLayout android:layout_weight="2" android:layout_height="fill_parent" android:id="@+id/linearLayout3" android:orientation="vertical" …
Run Code Online (Sandbox Code Playgroud)

layout android resize android-linearlayout

133
推荐指数
3
解决办法
25万
查看次数

软件键盘在Android上调整背景图像的大小

每当出现软件键盘时,它都会调整背景图像的大小.请参阅下面的屏幕截图:

如你所见,背景有点挤压.任何人都可以了解为什么背景调整大小?

我的布局如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/page_bg"
    android:isScrollContainer="false"
>
    <LinearLayout android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
    >
        <EditText android:id="@+id/CatName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:inputType="textCapSentences"
            android:lines="1"
        />
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:onClick="saveCat"
        />
    </LinearLayout>
    <ImageButton
        android:id="@+id/add_totalk"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:background="@null"
        android:src="@drawable/add_small"
        android:scaleType="center"
        android:onClick="createToTalk"
        android:layout_marginTop="5dp"
    />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android view android-linearlayout

127
推荐指数
8
解决办法
6万
查看次数

立即获取LinearLayout中的所有子视图

我有一个LinearLayout,其中包含几个孩子TextViews.如何使用循环获取该LinerLayout的子视图?

android android-linearlayout

127
推荐指数
4
解决办法
9万
查看次数

使用LinearLayout将按钮放在屏幕底部?

我有以下代码,如何使它3个按钮位于底部?

    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:gravity="center"
        android:text="@string/observer"
        android:textAppearance="?android:attr/textAppearanceLarge"
        tools:context=".asdf"
        android:weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="1" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="2" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal|center"
            android:text="3" />
    </LinearLayout>
Run Code Online (Sandbox Code Playgroud)

xml android button android-layout android-linearlayout

126
推荐指数
5
解决办法
29万
查看次数

Android:以编程方式将textview添加到线性布局

我试图TextViews在代码中添加到我的xml定义的布局.我有一个xml表,其中Views定义了很多.但是我必须在代码中添加一些视图,所以LinearLayout在xml-sheet中创建一个:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/info"
android:layout_height="wrap_content" 
android:orientation="vertical">
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在这个布局中,我想添加我的TextView:

    View linearLayout =  findViewById(R.id.info);
    //LinearLayout layout = (LinearLayout) findViewById(R.id.info);


    TextView valueTV = new TextView(this);
    valueTV.setText("hallo hallo");
    valueTV.setId(5);
    valueTV.setLayoutParams(new LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));

    ((LinearLayout) linearLayout).addView(valueTV);
Run Code Online (Sandbox Code Playgroud)

但我只收到以下错误消息:

: java.lang.ClassCastException: android.widget.TextView
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

谢谢你的帮助.马丁

java xml android textview android-linearlayout

122
推荐指数
6
解决办法
26万
查看次数

RelativeLayout比LinearLayout更昂贵吗?

每次我需要一个View容器时,我一直在使用RelativeLayout,因为它的灵活性,即使我只是想显示一些非常简单的东西.

这样做是否可以,或者我应该从性能/良好实践的角度尝试使用LinearLayout?

谢谢!

performance android android-linearlayout android-relativelayout

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

Android - 在LinearLayout中水平居中TextView

我有以下基本布局

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bar_background">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:padding="10dp"
        android:text="HELLO WORLD" />

    </LinearLayout>
<LinearLayout>
Run Code Online (Sandbox Code Playgroud)

似乎xml是正确的,但文本是左对齐的.textview占用父级的整个宽度,textview设置为居中.不确定是什么问题...

android android-layout android-linearlayout layout-gravity

105
推荐指数
4
解决办法
15万
查看次数

如何围绕Android LinearLayout创建边框?

我有一个大的布局,里面有一个较小的布局.

如何在小布局周围创建线条边框?

android android-layout android-linearlayout

101
推荐指数
5
解决办法
17万
查看次数

如果它大于1行,如何在TextView上显示省略号?

我有以下布局不起作用:

<LinearLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:id="@+id/experienceLayout" 
    android:background="#ffffff" 
    android:layout_height="match_parent" 
    android:paddingLeft="6dp" 
    android:paddingRight="6dp" 
    android:paddingBottom="6dp" 
    android:paddingTop="6dp">

    <TextView 
        android:layout_weight="1" 
        android:id="@+id/experienceLabel" 
        android:text="Experience" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:textStyle="bold">
    </TextView>

    <TextView 
        android:id="@+id/experienceTextView" 
        android:text="TextView" 
        android:layout_height="wrap_content" 
        android:textColor="#000000" 
        android:layout_width="wrap_content" 
        android:ellipsize="end" 
        android:lines="1" 
        android:maxLines="1" 
        android:singleLine="true" 
        android:fadeScrollbars="false">
    </TextView>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

java android textview android-linearlayout

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

如何将(垂直)分隔符添加到水平LinearLayout?

我正在尝试将分隔符添加到水平线性布局但是无处可去.分频器没有显示.我是Android的新手.

这是我的布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/llTopBar"
        android:orientation="horizontal"
        android:divider="#00ff00"
        android:dividerPadding="22dip"
        android:showDividers="middle"
       >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf" />
            <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="asdf"
             />

    </LinearLayout>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

android divider android-layout android-linearlayout

85
推荐指数
6
解决办法
15万
查看次数