2015-04-21 80 views
0

我是新来的Android和试图把我的应用程序块在一起。我正在努力找到一种方法将我的应用程序信息移动到屏幕的另一侧。我正在使用只有ListView/TextView的自定义布局,并且正在通过ArrayAdapter进行访问。有没有办法实现我想要的结果动态?即︰列表视图添加两个动态的TextViews,我假设用FindById更新TextViews,然后添加一个新的行,有一个新的项目,添加另外两个TextViews等..如果我需要提供额外的信息,请让我知道。Android的listview/textview布局

第一张截图(A)是它当前如何显示。第二张截图(B)是我希望如何显示的。

//customListView.xml 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:gravity="center_vertical" 
    android:minHeight="?android:attr/listPreferredItemHeightSmall" /> 

//main.java

where.add( “可用值:\ t” 的+ ac.get(1)+ “\ n” + “可用的现金:\ t” 的+ AC。得到(2));

where.add(“”); mMainListAdapter = new ArrayAdapter(this,R.layout.custom_listview,where);

屏幕快照:

屏幕截图B:What I want it to look like

回答

0

尝试LinearLayoutandroid:layout_weight安卓layout_weight决定谁将会占用空的空间。您应该使用多个TextView来实现您想要的输出。

<Button 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="1" /> 

<Button 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="3" 
    android:text="2" /> 

<Button 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="3" /> 

</LinearLayout> 

输出

enter image description here

对于您的情况:

<LinearLayout 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" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.test.MainActivity" > 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:text="Available Value"   
     android:layout_weight="1" /> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="$314.10" /> 

</LinearLayout> 

输出如下:

enter image description here

+0

在按钮为“2”的情况下,空白控件中会出现什么?你是否说我需要创建额外的TextView,尽管我可能不会使用它们? – snapplex

+0

@snapplex“空管”是什么意思? –

+0

如果我使用你的例子,第一个按钮将有“可用值:”,第二个按钮将是空白的... ...第三个按钮将有314美元。10 - 也许我误解了你的意思。 – snapplex

0

你可以尝试这样的布局,基于RelativeLayout

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/relativeLayout1"> 
     <TextView 
      android:text="Available cash" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView1" 
      android:layout_toLeftOf="@+id/textView2" /> 
     <TextView 
      android:text="$314.44" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/textView2" 
      android:layout_alignParentRight="true" /> 
    </RelativeLayout> 
0

对于每一行,你将需要2个TextViews,一个文本,一个用于数字。 因此,要获得您的屏幕,您将需要总共4个TextViews。 文本将相对于父项左对齐,数字将右对齐。父母。

我已经在下面给出了一个示例,试一下,让我知道它是否工作。你可能需要稍微调整一下。

<RelativeLayout> 

    <TextView 
     android:id="@+id/Available" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Available Value" 
      android:textStyle="bold" 
      android:layout_alignParentLeft="true"/> 

    <TextView 
     android:id="@+id/AvailableValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/Value" 
      android:textStyle="bold" 
      android:layout_alignParentRight="true”/> 

    <TextView 
     android:id="@+id/tvCash" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Available Value" 
      android:textStyle="bold" 
      android:layout_alignParentLeft=“true" 
     android:layout_below=android:id="@id/Available" 
/> 

    <TextView 
     android:id="@+id/tvCashValue" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/Value" 
      android:textStyle="bold" 
      android:layout_alignParentRight="true"/> 


    </RelativeLayout> 
+0

所以,这听起来像我需要动态创建和重新调整textViews之前,我添加额外的项目到我的ListView? – snapplex

+0

如果上面是每个列表的一行,您不必。您将为列表的每一行创建一个布局,并在适配器的getView()方法中将其充气。 – TechnoBlahble

+0

您可以参考http://www.android-ios-tutorials.com/android/android-custom-listview-例/ – TechnoBlahble