如何在不使用重力属性的情况下移动右侧的第一列?
Posted
技术标签:
【中文标题】如何在不使用重力属性的情况下移动右侧的第一列?【英文标题】:How can I move the first column on the right side without using gravity property? 【发布时间】:2016-11-27 23:38:04 【问题描述】:在桌子上,我尝试将红色列从右侧移动到左侧,并有可能旋转。
我尝试使用android:gravity="right"
,但没有结果。
如何将绿色列移动到最右侧并将其他列移动到左侧而不会出现问题。
这是screenshot
我的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_
android:layout_
android:gravity="right"
android:id="@+id/fillable_area">
<TableLayout
android:gravity="right"
android:id="@+id/table_header"
android:layout_
android:layout_/>
<ScrollView
android:layout_
android:layout_>
<LinearLayout android:orientation="horizontal"
android:layout_
android:layout_
android:gravity="right"
>
<TableLayout
android:gravity="right"
android:id="@+id/scrollable_part2"
android:layout_
android:layout_/>
<HorizontalScrollView
android:gravity="left"
android:id="@+id/scroller"
android:layout_
android:layout_>
<TableLayout
android:gravity="left"
android:id="@+id/scrollable_part"
android:layout_
android:layout_/>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
我的代码:
package com.examp.swap_items;
import java.util.ArrayList;
import android.R.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity
private boolean showSummaries;
private TableLayout summaryTable;
private TableLayout frozenTable;
private TableLayout contentTable;
private Button backButton ;
private HorizontalScrollView hor;
private TextView recyclableTextView;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableRow.LayoutParams wrapWrapTableRowParams = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int[] fixedColumnWidths = new int[]20, 20,20, 20, 20;
int[] scrollableColumnWidths = new int[]20, 20, 20, 30, 30;
int fixedRowHeight = 50;
int fixedHeaderHeight = 60;
TableRow row = new TableRow(this);
//header (fixed vertically)
TableLayout header=(TableLayout)findViewById(R.id.table_header);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.CENTER);
row.setBackgroundColor(Color.WHITE);
row.addView(makeTableRowWithText("col1", fixedColumnWidths[0],fixedHeaderHeight));
row.addView(makeTableRowWithText("col2", fixedColumnWidths[1],fixedHeaderHeight));
row.addView(makeTableRowWithText("col3", fixedColumnWidths[2],fixedHeaderHeight));
row.addView(makeTableRowWithText("col4", fixedColumnWidths[3],fixedHeaderHeight));
row.addView(makeTableRowWithText("col5", fixedColumnWidths[4],fixedHeaderHeight));
header.addView(row);
hor = (HorizontalScrollView)findViewById(R.id.scroller);
hor.postDelayed(new Runnable()
public void run()
hor.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
, 1L);
TableLayout fixedColumn = (TableLayout) findViewById(R.id.scrollable_part2);
//rest of the table (within a scroll view)
TableLayout scrollablePart = (TableLayout)findViewById(R.id.scrollable_part);
for(int i = 0; i < 10; i++)
TextView fixedView = makeTableRowWithText("fixed number " + i, scrollableColumnWidths[0], fixedRowHeight);
fixedView.setBackgroundColor(Color.GREEN);
fixedView.setGravity(Gravity.RIGHT);
fixedColumn.addView(fixedView);
row = new TableRow(this);
row.setLayoutParams(wrapWrapTableRowParams);
row.setGravity(Gravity.LEFT);
row.setBackgroundColor(Color.RED);
row.addView(makeTableRowWithText("scroll 1", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("scroll 2", scrollableColumnWidths[1], fixedRowHeight));
row.addView(makeTableRowWithText("scroll 3", scrollableColumnWidths[2], fixedRowHeight));
row.addView(makeTableRowWithText("scroll 4", scrollableColumnWidths[3], fixedRowHeight));
row.addView(makeTableRowWithText("scroll 5", scrollableColumnWidths[4], fixedRowHeight));
scrollablePart.addView(row);
public TextView makeTableRowWithText(String text, int widthInPercentOfScreenWidth, int fixedHeightInPixels)
int screenWidth = getResources().getDisplayMetrics().widthPixels;
recyclableTextView = new TextView(this);
recyclableTextView.setText(text);
recyclableTextView.setTextColor(Color.BLACK);
recyclableTextView.setTextSize(20);
recyclableTextView.setWidth(widthInPercentOfScreenWidth * screenWidth / 100);
recyclableTextView.setHeight(fixedHeightInPixels);
return recyclableTextView;
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
【问题讨论】:
【参考方案1】:重力不是这个问题的解决方案,android:gravity
属性会移动元素的内容,这就是为什么您可以看到所有“固定”文本都向右对齐的原因柱子的手边。
LinearLayout
中子项的顺序完全取决于它们在布局 xml 中出现的顺序。在垂直布局中,它们按从上到下的顺序排列,在水平布局中,它们按从左到右的顺序排列。所以在你的情况下,如果你想改变这些列的顺序,你只需要在你的布局中以正确的顺序放置它们:
<LinearLayout android:orientation="horizontal"
android:layout_
android:layout_>
<HorizontalScrollView
android:id="@+id/scroller"
android:layout_
android:layout_weight="1"
android:layout_>
<TableLayout
android:id="@+id/scrollable_part"
android:layout_
android:layout_/>
</HorizontalScrollView>
<TableLayout
android:id="@+id/scrollable_part2"
android:layout_
android:layout_/>
</LinearLayout>
【讨论】:
谢谢,你试过代码了吗?因为我修改代码后绿柱消失了变成只有红柱现在只有红柱现在有什么合适的解决方案请教 啊,是的,Horizontal ScrollView 想要占据整个屏幕。我将使用 android:weight 为 ScrollViews 宽度编辑答案。然后它将只使用 Fixed 列未使用的任何空间 谢谢你,好的等你 已经改变了^^ Horizontal ScrollView 更新了 layout_width 和 layout_weight以上是关于如何在不使用重力属性的情况下移动右侧的第一列?的主要内容,如果未能解决你的问题,请参考以下文章
C# - 如何在不使用索引器的情况下获取自定义集合的第一项?